what does 'a=b=c=[]' do
Thomas Rachel
nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Sat Dec 24 13:49:53 EST 2011
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
More information about the Python-list
mailing list