為什么輸出L2、L3時,后面插入的字符串還是重復(fù)插入,明明設(shè)置不是一個隊列內(nèi)?
# Enter a code
L=['Alice','Bob','Candy','David','Ellena']
L1 = L
L1.append('Gen')
L2 = L
L1.append('Phoebe')
L1.append('Zero')
print(L1)
L2.append('Zero')
L2.insert(6,'Phoebe')
print(L2)
L3=['Gen','Phoebe','Zero']
print(L+L3)
2024-02-26
list = [1, 2, 3]
list1 = list
list1.append(4)
print('list1', list1)
print('list', list)
list1 [1, 2, 3, 4]
list [1, 2, 3, 4]
要避免這種情況,使用copy,如:
list2 = list.copy()
print('list2', list2)
list2.append(100)
print('list2', list2)
print('list', list)
list2 [1, 2, 3, 4]
list2 [1, 2, 3, 4, 100]
list [1, 2, 3, 4]