CodePudding user response:
I think it should be alist. Append is pointed to the blist space, while may optimize the python code, and then to generate alistAccording to your program logic, is suitable to this
Def get_input () :
Name=input (" name: ")
Id=input (', ')
Return [name, id]
I=0
Alist=[]
While i<2:
Blist=get_input ()
Alist. Append (blist)
I=I + 1
Print (alist)
CodePudding user response:
Actually I also is to compare we want it to memory spaceCodePudding user response:
That is the id check blist
Def get_input () :
Name=input (" name: ")
Id=input (', ')
Return [name, id]
I=0
Alist=[]
While i<2:
Blist=get_input ()
Print (' times {} blist id '. The format (I), id (blist))
Alist. Append (blist)
I=I + 1
Print (alist)
Here id is different every time, point to different storage space, your code, check the blist id, is pointing at the same address,
CodePudding user response:
This type of container for parameter list, dictionary, is the address, not copy theYou write a function to modify the list, the incoming parameter list, the list will be changed,
Want to copy, use slice
Alist. Append (blist [:])
CodePudding user response:
This copy with your writing and depth are concerned, you can copy, depth theatre list variable types, like you write alist [0], alist [1] to the value of the memory address is the same, no matter you have alist [0] or alist [1] modified, the modified value, is a memory address is the blist defined at the beginning, you want to achieve your results can be used to deep copy, so that you can achieve the effect of you want to have, to a deep copy [0], a [1] open new memory every time,The from copy import deepcopy
I=0
Alist=[]
Clist=[]
Blist=[" ", ""]
Print (id (blist))
While I & lt; 2:
Blist [0]=input (" name ")
Blist [1]=input (" age ")
Print (id (blist))
D=deepcopy (blist)
(d) print (id)
Clist. Append (blist)
Alist. Append (d)
I=I + 1
Print (clist)
Print (id (clist [0]), id (clist [1]))
Print (alist)
Print (id (alist [0]), id (alist [1]))
You can change kind of writing, each time to initialize the value of the blist
I=0
Alist=[]
While I & lt; 2:
Blist=[]
Print (id (blist))
Blist. Append (input (" name "))
Blist. Append (input (" age "))
Alist. Append (blist)
I +=1
Print (alist)
Print (id (alist [0]), id (alist [1]))
CodePudding user response:
List is equivalent to the nature of the use of the pointer in C language, a list of name is a pointer to an area that can be understood as the address of a building, for a list of the append function are equivalent to the last element of the list of assignment, if the object is a list of assignment is pretty so the list of addresses to the assigned object,In your program, alist. Append (blist) this statement is equivalent to put blist list address alist of the position of the first and the second element, that is to say, these two elements is pointing to the same storage space, so the result of your program is running will is the last time the input values,
If you want to achieve copy list value instead of address, you can use
1. Slice, namely alist. Append (blist [:])
2. The copy function, namely alist. Append (blist. Copy ())
3. The list will blist statement in the loop, at this time of every cycle blist to apply for a storage space, the natural points to address is different also,