Home > other >  Python is a small white meng made online
Python is a small white meng made online

Time:09-28

Why Python and c is not the same, should not be just a different value,

CodePudding user response:

Congratulations to you set foot in the pit of the python list is initialized, to feel the:

 

Print (' pit of three elements, the address is the same, changed j [0], j [1], [2] j also changed, ')
J=] of [[0] * 2 * 3
For x in j:
Print (id (x))
J [0] [0]=1
Print (j)

Print (' can ')
K=[[0, 0], [0, 0], [0, 0]]
For x in k:
Print (id (x))
K [0] [0]=1
Print (k)

Print (' in ')
L=[[0 for n in range (2)] for m in range (3)]
For x in l:
Print (id (x))

L [0] [0]=1
Print (l)




CodePudding user response:

 
> J=[[0, 0], [0, 0], [0, 0]]
> J
[[0, 0], [0, 0], [0, 0]]
> J [0] [0]=1
> J
[[1, 0], [0, 0], [0, 0]]
> For the row in j:
. Print (id (row))
.
140111482549512
140111482550472
140111481701512
> J=] of [[0] * 2 * 3
> J
[[0, 0], [0, 0], [0, 0]]
> For the row in j:
. Print (id (row))
.
140111481702024
140111481702024
140111481702024
>


You use j=[2] [0] * * 3 to create the list each element is the same address, the value of the corresponding change, will be changed

CodePudding user response:

reference 1/f, old coconut response:
congratulations you step into the pit of the python list is initialized, to feel the:

 

Print (' pit of three elements, the address is the same, changed j [0], j [1], [2] j also changed, ')
J=] of [[0] * 2 * 3
For x in j:
Print (id (x))
J [0] [0]=1
Print (j)

Print (' can ')
K=[[0, 0], [0, 0], [0, 0]]
For x in k:
Print (id (x))
K [0] [0]=1
Print (k)

Print (' in ')
L=[[0 for n in range (2)] for m in range (3)]
For x in l:
Print (id (x))

L [0] [0]=1
Print (l)



Thank you thank you thank you thank you thank you thank you thank you thank you thank you

CodePudding user response:

refer to the second floor mathematical slasher response:
 
> J=[[0, 0], [0, 0], [0, 0]]
> J
[[0, 0], [0, 0], [0, 0]]
> J [0] [0]=1
> J
[[1, 0], [0, 0], [0, 0]]
> For the row in j:
. Print (id (row))
.
140111482549512
140111482550472
140111481701512
> J=] of [[0] * 2 * 3
> J
[[0, 0], [0, 0], [0, 0]]
> For the row in j:
. Print (id (row))
.
140111481702024
140111481702024
140111481702024
>


You use j=[2] [0] * * 3 to create the list each element is the same address, corresponding values changed, it will change the

Thank you thank you thank you thank you thank you thank you thank you thank you at all
  • Related