Am 21.12.2011 23:25 schrieb Eric:

Is it true that if I want to create an array or arbitrary size such
as:
    for a in range(n):
       x.append(<some function...>)

I must do this instead?
    x=[]
    for a in range(n):
       x.append(<some function...>)

Of course - your x must exist before using it.

> Now to my actual question.  I need to do the above for multiple arrays
(all the same, arbitrary size).  So I do this:
    x=y=z=[]
    for a in range(n):
       x.append(<some function...>)
       y.append(<some other function...>)
       z.append(<yet another function...>)
Also, is there a more pythonic way to do "x=[], y=[], z=[]"?

You could do:

def create_xyz(n):
    for a in range(n):
        yield <some function...>, <some other function...>, \
                <yet another function...>)

x, y, z = zip(*create_xyz(11))

or, if you want x, y, z to be lists,

x, y, z = [list(i) for i in zip(*create_xyz(11))]

.


Thomas
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to