python - Appending data to lists of multiple elements appends it to all element's lists -
given following sample code:
class parent: some_list = [] # type: list[int] def __init__(self, id): self.id = id if __name__ == '__main__': list_of_parents = [] # type: list[parent] # create 10 parents in range(10): list_of_parents.append(parent(i)) # go on each parent , add 10 integers each of lists i, p in enumerate(list_of_parents): j in range(10): list_of_parents[i].some_list.append(1)
after executing code, there list of 10 parent
objects, each of them have list 100 entries. (consisting of 10 times integers 0
-9
), though j
elements explicitly added list_of_parents[i]
.
what's reason this?
it's moses koledoye mentioned. try , see difference:
class parent: def __init__(self, id): self.id = id self.some_list = []# type: list[int] if __name__ == '__main__': list_of_parents = [] # type: list[parent] # create 10 parents in range(10): list_of_parents.append(parent(i)) # go on each parent , add 10 integers each of lists i, p in enumerate(list_of_parents): j in range(10): list_of_parents[i].some_list.append(1) print(list_of_parents[i].some_list) print("__________")
output:
[1] __________ [1, 1] __________ [1, 1, 1] __________ [1, 1, 1, 1] __________ [1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1] __________ [1, 1] __________ [1, 1, 1] __________ [1, 1, 1, 1] __________ [1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1] __________ [1, 1] __________ [1, 1, 1] __________ [1, 1, 1, 1] __________ [1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1] __________ [1, 1] __________ [1, 1, 1] __________ [1, 1, 1, 1] __________ [1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1] __________ [1, 1] __________ [1, 1, 1] __________ [1, 1, 1, 1] __________ [1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1] __________ [1, 1] __________ [1, 1, 1] __________ [1, 1, 1, 1] __________ [1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1] __________ [1, 1] __________ [1, 1, 1] __________ [1, 1, 1, 1] __________ [1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1] __________ [1, 1] __________ [1, 1, 1] __________ [1, 1, 1, 1] __________ [1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1] __________ [1, 1] __________ [1, 1, 1] __________ [1, 1, 1, 1] __________ [1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1] __________ [1, 1] __________ [1, 1, 1] __________ [1, 1, 1, 1] __________ [1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1] __________ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] __________
Comments
Post a Comment