Closed kckennylau closed 7 years ago
This is not a bug related with X
.
It's just the way Python handles objects. Try this Python code:
>>> a = [[1, 2]] * 2
>>> a[0][0] = 100
>>> a
[[100, 2], [100, 2]]
Python doesn't actually copy elements, when you don't be explicit. It's the same thing as a = b
. There is still only one object, not two. a
and b
simply point to the same object.
mU5 3
works though, since you generate three completely different lists.
We probably want to change the behavior of *
. It should deepcopy the elements (like we do in =
or ~
).
"We"? How many developers are there?
Also, is there a golfier way to modify elements in 2D lists?
Please look at the following two codes:
[
J*\]U5 3XJ0X@J0T5
](http://pyth.herokuapp.com/?code=J%2a%5DU5+3XJ0X%40J0T5&debug=0) (The\]
is not really there, but I cannot type bare]
in link text.)JmU5 3XJ0X@J0T5
The only difference is how
J
is generated, but the end result is different.Namely, in the first code, all the sub-arrays are replaced.