在 Python 中,我们可以使用
L
set(L)
如果列表中的所有项目都是hashable,那么字典可以用于保序重复数据删除:
L = list(dict.fromkeys(L))
对于较旧的 Python 版本(<= 3.6) where dictionaries don't preserve ordering, you can do the same thing using a
collections.OrderedDict
如果任何列表项不可散列,将会有一个
TypeError
l = [] for item in list_: if item not in l: l.append(item)
对于非常大的、多样化的
list_