On Wed, Dec 21, 2011 at 2:25 PM, Eric <<a href="mailto:einazaki668@yahoo.com">einazaki668@yahoo.com</a>> wrote:<br>><br>> Is it true that if I want to create an array or arbitrary size such<br>> as:<br>> for a in range(n):<br>
> x.append(<some function...>)<br>><br>> I must do this instead?<br>> x=[]<br>> for a in range(n):<br>> x.append(<some function...>)<br><br>You can also use a list comprehension:<br>
<blockquote style="margin:0 0 0 40px;border:none;padding:0px">x = [<some function...> for a in range(n)]</blockquote>Or extend and a generator expression:<br><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
x = []</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">x.extend(<some function...> for a in range(n))</blockquote>Or map and a generator function:<br><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
map(x.append, (<some function...> for a in range(n)))</blockquote><br>I would recommend either your way, the first of, or the second of my ways, depending on the full context.<br> <br>><br>><br>> Now to my actual question. I need to do the above for multiple arrays<br>
> (all the same, arbitrary size). So I do this:<br>> x=y=z=[]<div><br></div><div>This creates a new object, then assigns the labels x, y, and z to that object.</div><div><br>> for a in range(n):<br>> x.append(<some function...>)<br>
> y.append(<some other function...>)<br>> z.append(<yet another function...>)</div><div><br></div><div>Then this appends the items to each of those labels, which, as they pointing to the same object, appends to all of the labels. The "variables" in Python are merely labels, and assigning to different labels does not automatically copy the object.</div>
<div><br></div><div>Consider:</div><div>a = []</div><div>b = a</div><div>a.append(1)</div><div>print b</div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>[1]</div></blockquote><div><br></div><div>><br>
> Except it seems that I didn't create three different arrays, I created<br>> one array that goes by three different names (i.e. x[], y[] and z[]<br>> all reference the same pile of numbers, no idea which pile).<br>
><br>> This surprises me, can someone tell me why it shouldn't? I figure if<br>> I want to create and initialize three scalars the just do "a=b=c=7",<br>> for example, so why not extend it to arrays. Also, is there a more<br>
> pythonic way to do "x=[], y=[], z=[]"?</div><div><br></div><div>The above rules apply in all cases, however are generally invisible on immutable objects (strings, ints, floats, tuples). In the case of a=b=c=7, you will find that all of a, b, and c point to the same object (try the "id" function or "is" operator). Doing the operation a += 1 after a=7 will create a new int* with the value 7+1 and assign it to the label a.</div>
<div><br></div><div>* In CPython, there exists an optimization where small ints are cached, namely from -7 to 255 (the lower bound I stated may be wrong). This improved performance in most cases, but is CPython specific - other implementations such as PyPy or IronPython may behave differently.</div>
<div><br>><br>> It's a slick language but I still have trouble wrapping my brain<br>> around some of the concepts.<br>><br>> TIA,<br>> eric<br>> --<br>> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br>
<br></div>