Variable Scope 2 -- Thanks for 1.

Francis Avila francisgavila at yahoo.com
Sun Jan 11 14:20:59 EST 2004


Francis Avila wrote in message <10038cpbu23hv93 at corp.supernews.com>...
>Jens Thiede wrote in message ...
>>OK, thanks for sorting that out, but what do I replace in the
>>following to avoid referancing:
>>
>>x = [[0]*5]*5
>>x is [[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]]
>>
>>and after
>>
>>x[0][0] = 1;
>>x is [[1,0,0,0,0], [1,0,0,0,0], [1,0,0,0,0], [1,0,0,0,0], [1,0,0,0,0]]
>
>Ah, the joy of list comprehensions!
>
>>>> x = [ [ 0 for i in range(5)] for j in range(5)]

In my enthusiasm for list comprehensions I went too far.  The inner loop
comp is unnecessary because ints are immutable.  So  [ [0]*5 for i in
range(5) ] is enough.

--
Francis Avila




More information about the Python-list mailing list