Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 25th, 2012, 02:37 AM
evol evol is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 evol User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 10 sec
Reputation Power: 0
Outout of array

I've few questions.
1. How to generate float numbers in an array?
I use something like that
Code:
import array
import random
a=array.array('i',(random.randint(-50,51) for i in xrange(10)));
print a

This gives integer [-50; 50], but how to get float, for example, [-1, 1], not [-1, 1)?
2. How to output array in columns?
For example, I have generated array and I want to make output in 3 columns, how can I do it?
3. How to output array in rows?
Same that with columns, but, for example, in 10 rows.

Reply With Quote
  #2  
Old December 25th, 2012, 10:53 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,353 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 8 h 3 m 36 sec
Reputation Power: 383
numpy handles arrays more elegantly than does plain old python. Look for the enthought python distribution, or scipy.org

>>> random.random()
0.95659562170489

You might have to make lists of array to get "tables" using array. Yes, arrays don't support a "collection type".
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old December 26th, 2012, 02:50 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 480 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 57 sec
Reputation Power: 63
One way to do that:
Code:
''' array_floats1.py
create a Python array of 10 random floats between -1 and 1
print the elements with different options
'''

# allows Python27 to use Python3 print() options
from __future__ import print_function

import array
import random as rn

arr = array.array('f',[rn.random()*rn.choice([-1,1]) for i in range(10)])

# print 3 items max per row
count = 1
for item in arr:
    #print(count, count % 3)
    if count % 3 == 0:
        print("%10.6f" % item, end="\n")
    else:
        print("%10.6f" % item, end=" ")
    count += 1

print("")
    
print('-'*32)

# print 1 item per row
for item in arr:
    print("%10.6f" % item)

''' potential output -->
 -0.357831  -0.926154   0.836476
  0.317901  -0.845858  -0.940639
  0.895811   0.133088   0.346653
  0.618086 
--------------------------------
 -0.357831
 -0.926154
  0.836476
  0.317901
 -0.845858
 -0.940639
  0.895811
  0.133088
  0.346653
  0.618086
'''
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Outout of array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap