May I ask why in Python, when I use a for loop to assign a value to a nested list, it is always calculated based on the final value of i?
扔个三星炸死你
扔个三星炸死你 2017-06-12 09:25:09
0
3
913

There are two examples, the first one is as follows,

a=[0]*5
for i in range(5):
    a[0]=i+3

At this time, a=[3,4,5,6,7]
The second one is as follows:

a=[[0,0]]*5
for i in range(5):
    a[0]=i+3

At this time, a=[[7, 0], [7, 0], [7, 0], [7, 0], [7, 0]]

Why does this happen? Is there anything wrong with my second way of writing?
Newbies, please give me some advice!

扔个三星炸死你
扔个三星炸死你

reply all(3)
大家讲道理

You have made two problems here:
The first one, as mentioned above, you have been modifying the value of a[0], and you have not put the changed i into the list for processing, or in other words, You missed the code to write i into the question:
Correct method:

a = [0] * 5
for i in range(5):
    a[i] = i + 3
print a

The second question, which is what you asked above, is why a=[[0,0]]*5This definition method turns out to be a=[[7, 0], [7 , 0], [7, 0], [7, 0], [7, 0]]
This question has one thing in common with the first question, that is, you probably forgot to write a[i][ 0] = i + 3,
The second is: If you use [[0, 0]] * 5 to generate a list, everything in it is a reference, they are the same object, not 5 Object! See example:

a = [[0, 0]] * 5
print a
print id(a[0])
print id(a[1])
print id(a[2])

# 输出
[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
42455688
42455688
42455688

You can see from the idvalue that they all have the same address, so the 5 objects in the list are all the same, so when you execute a[i][0]= i+3, no matter No matter which element you modify, you will end up modifying the same list!
So if you want to try the effect you want, you can’t use that method to quickly generate a list, you can only use the following method:

a = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], ]
for i in range(5):
    a[i][0] = i + 3
print a

# 输出
[[3, 0], [4, 0], [5, 0], [6, 0], [7, 0]]
漂亮男人

Because you need to use the i variable to iterate, if you always change 0, a[0] will of course be overwritten, and it will be the last value
In the first code, you cannot get a=[3,4,5, 6,7], you have to use the i variable

In [156]: a=[0]*5 
 ...: for i in range(5):
 ...:     a[0]=i+3
 ...:

In [157]: a
Out[157]: [7, 0, 0, 0, 0]

In [159]: a=[0]*5
     ...: for i in range(5):
     ...:     a[i]=i+3
     ...:
     ...:

In [160]:

In [160]: a
Out[160]: [3, 4, 5, 6, 7]

Second code:

In [163]: a=[[0,0]]*5
     ...: for i in range(5):
     ...:     a[0]=i+3
     ...:

In [164]: a
Out[164]: [7, [0, 0], [0, 0], [0, 0], [0, 0]]

In [165]: a=[[0,0]]*5
     ...: for i in range(5):
     ...:     a[i]=i+3
     ...:
     ...:

In [166]: a
Out[166]: [3, 4, 5, 6, 7]

It seems that you wrote the code wrong, I guess you want to ask this question

In [168]: a=[[0,0]]*5
 ...: for i in range(5):
 ...:     a[i][0]=i+3
 ...:
 ...:
 ...:

In [169]: a
Out[169]: [[7, 0], [7, 0], [7, 0], [7, 0], [7, 0]]

You can print out a

In [175]: a=[[0,0]]*5
 ...: for i in range(5):
 ...:     a[i][0]=i+3
 ...:     print(a,id(a[i]))
 ...:
[[3, 0], [3, 0], [3, 0], [3, 0], [3, 0]] 93411808
[[4, 0], [4, 0], [4, 0], [4, 0], [4, 0]] 93411808
[[5, 0], [5, 0], [5, 0], [5, 0], [5, 0]] 93411808
[[6, 0], [6, 0], [6, 0], [6, 0], [6, 0]] 93411808
[[7, 0], [7, 0], [7, 0], [7, 0], [7, 0]] 93411808

@Lin_R is right

phpcn_u1582

In fact, the second method is shared, not separate. Because it is a list at this time and is variable, while the first method is a number and is immutable.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!