我正在研究这些功能(见this):
def removeFromList(elementsToRemove):
def closure(list):
for element in elementsToRemove:
if list[0] != element:
return
else:
list.pop(0)
return closure
def func(listOfLists):
result = []
for i, thisList in enumerate(listOfLists):
result.append(thisList)
map(removeFromList(thisList), listOfLists[i+1:])
return result
我有一个想要作为参数传递的列表,但我希望这个列表保持不变。我尝试的是:
my_list = [[1], [1, 2], [1, 2, 3]]
print my_list
#[[1], [1, 2], [1, 2, 3]]
copy_my_list = list (my_list)
#This also fails
#copy_my_list = my_list [:]
print id (my_list) == id (copy_my_list)
#False
print func (copy_my_list)
#[[1], [2], [3]]
print my_list
#[[1], [2], [3]]
但它确实改变了我原来的清单。有任何想法吗?