0

When using * to create list like:

>>> ll = [None] * 3
>>> ll
[None, None, None]
>>> ll[0] = 2
>>> ll
[2, None, None]

And then using two dimension,

>>> ll2 = 3 *[ 3 * [None]]
>>> ll2
[[None, None, None], [None, None, None], [None, None, None]]
>>> ll2[0][0] = 2
>>> ll2
[[2, None, None], [2, None, None], [2, None, None]]

So, create list by operator *, if the element is object like list. It doesn't create new object, just use the referenced object.

Are there any other operators like this in Python?

Edit:

Sorry, My representation is bad. What I want to know is, are there any other operators using like "*" will not create multiple reference.

Yanhui Zhou
  • 872
  • 6
  • 20
  • 1
    *Are there any other operators like this* - like what? – thefourtheye Mar 21 '16 at 05:22
  • StackOverflow isn't Google. Google is Google. – Debosmit Ray Mar 21 '16 at 05:32
  • ​​​​​​​​​​​​​​​To all the answerers - I think OP's asking about: *Are there any other operators like this in Python?* **Not:** *What's wrong with my code and how can I fix it?* – Remi Guan Mar 21 '16 at 05:39
  • @KevinGuan Thanks a lot. – Yanhui Zhou Mar 21 '16 at 07:28
  • @Rogalski That problem is what I know. And What I ask is, are there any other operators using like "*" will not create multiple reference. – Yanhui Zhou Mar 21 '16 at 07:40
  • [How to define two-dimensional array in python](http://stackoverflow.com/questions/6667201/how-to-define-two-dimensional-array-in-python), [How to initialize a two-dimensional array in Python?](http://stackoverflow.com/questions/2397141/how-to-initialize-a-two-dimensional-array-in-python) – GingerPlusPlus Mar 21 '16 at 08:31
  • @GingerPlusPlus That is nothing to do with my problem. – Yanhui Zhou Mar 21 '16 at 09:45

4 Answers4

6

If you multiply the list of lists, it will create multiple reference of the inner lists.

>>> lst = 3 * [3 * [None]]
>>> lst[0] is lst[1]
True
>>> lst[0] is lst[2]
True

You need to create separated lists. For example, using list comprehension:

>>> lst = [3 * [None] for i in range(3)]
>>> lst[0][0] = 2
>>> lst
[[2, None, None], [None, None, None], [None, None, None]]
falsetru
  • 357,413
  • 63
  • 732
  • 636
2

This has nothing to do with the * operator, but with the fact that you are using a list. [3 * [None]] creates a single list object, and you store a list of 3 references to this list in ll2. If you change one of the referenced lists, you change all the others, too. This behaviour will happen with all mutable objects

Jan Christoph Terasa
  • 5,781
  • 24
  • 34
1

This has nothing to do with the use of the * operator for multiplying a list. The assignment is by reference or in other words l is a mutable alias. To demonstrate the point, the following code will do the same thing.

l = [None, None, None]
l12 = [l, l, l]
print l12
>>> [[None, None, None], [None, None, None], [None, None, None]]
l[0]=2
print l12
>>> [[2, None, None], [2, None, None], [2, None, None]]
Spade
  • 2,220
  • 1
  • 19
  • 29
0

list comprehension

>>> ll=[[None for r in range(3)] for k in range(3)]
>>> ll[0][0]=3
>>> ll
[[3, None, None], [None, None, None], [None, None, None]]
python必须死
  • 999
  • 10
  • 12