看来你想维持秩序。在这种情况下,您可以保留一个集合来跟踪已添加的列表。
这是一个例子:
mylist = [['xxx', 879], ['yyy', 315], ['xxx', 879], ['zzz', 171], ['yyy', 315]]
# set that keeps track of what elements have been added
seen = set()
no_dups = []
for lst in mylist:
# convert to hashable type
current = tuple(lst)
# If element not in seen, add it to both
if current not in seen:
no_dups.append(lst)
seen.add(current)
print(no_dups)
哪个输出:
[['xxx', 879], ['yyy', 315], ['zzz', 171]]
Note:由于列表不可散列,因此您可以将元组添加到seen
set.