从 Python 中的列表中删除重复项

2023-09-19 15:29:54

listSet 之间的区别在于,一个元素可以在列表中出现多次,但一个元素在一个集中只能出现一次。因此,如果我们将列表转换为集合,则会删除重复项。 但是,不能保证元素的原始顺序。集合中元素的顺序由散列机制决定,散列机制可能与列表中的散列机制不同。这通过以下代码进行验证:

示例: List to Set

>>> mylist=[5,10,15,20,3,15,25,20,30,10,100]
>>> myset=set(mylist)
>>> print(list(myset))
[3, 100, 5, 10, 15, 20, 25, 30]

那么,如何删除重复的外观同时保留原始订单?

使用 For 循环将唯一项追加到另一个列表中(Append Unique Items in another List using For Loop)

一个简单的方法是将每个数字的首次出现附加到另一个列表中,如下所示。

示例: Append Unique Item into Another List

>>> uniques=[]
>>> for num in mylist:
    if num not in uniques:
        uniques.append(num)
>>> print(uniques)
[5, 10, 15, 20, 3, 25, 30, 100]

使用列表理解(Using List Comprehension)

我们可以使用list comprehension使其更简洁。

示例:

>>> uniques=[]
>>> [uniques.append(num) for num in mylist if not num in uniques] 
>>> print(uniques)
[5, 10, 15, 20, 3, 25, 30, 100]

上述方法实现简单,但效率不高,特别是对于包含大量项目的列表。以下技术相当有效地删除重复项。

Using OrderedDict.fromkeys()

该解决方案与低于 3.7 及更高版本的 Python 版本略有不同。在 Python 3.7 之前,字典输出可能不符合插入顺序。 但是,OrderedDict可以这样做。 然后,我们使用 fromkeys() 方法构建一个有序字典,该字典使用列表项作为关联值为 None 的键。

示例: OrderedDict.fromkeys()

>>> mylist=[5,10,15,20,3,15,25,20,30,10,100]
>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys(mylist))
[5, 10, 15, 20, 3, 25, 30, 100]

.box-4-multi-148{border:none !important;display:block !important;float:none !important;line-height:0px;margin-bottom:15px !important;margin-left:auto !important;margin-right:auto !important;margin-top:15px !important;

在更高版本中,字典保证记住其键插入顺序。因此,普通字典类的fromkeys()方法也可以完成相同的工作。

使用 reduce() 函数(Using the reduce() Function)

解决此问题的最有效方法是使用 functools 模块的reduce()功能。

在下面的示例中,具有空列表和集合的双元素元组用作初始值设定项。 原始列表中的每个新实例都追加到空列表中,而 Set 充当查找表。

示例: reduce()

>>> from functools import reduce
>>> mylist=[5,10,15,20,3,15,25,20,30,10,100]
>>> tup = (list(), set())
>>> # list to keep order, set as lookup table
>>> def myfunction(temp, item):
    if item not in temp[1]:
        temp[0].append(item)
        temp[1].add(item)
    return temp
>>> uniques=reduce(myfunction, mylist, tup)[0]
>>> print(uniques)
[5, 10, 15, 20, 3, 25, 30, 100]


本文内容总结: