python 删除重复项_Python从列表中删除重复项

python 删除重复项

There are many ways to remove duplicates from a Python List.

有很多方法可以从Python列表中删除重复项。

从列表中删除重复项 (Removing Duplicates from a List)

Python list can contain duplicate elements. Let’s look into examples of removing the duplicate elements in different ways.

Python列表可以包含重复的元素。 让我们看一下以不同方式删除重复元素的示例。

1.使用临时列表 (1. Using Temporary List)

This is the brute-force way to remove duplicate elements from a list. We will create a temporary list and append elements to it only if it’s not present.

这是从列表中删除重复元素的蛮力方法。 我们将创建一个临时列表,并仅在不存在临时列表时才添加元素。

ints_list = [1, 2, 3, 4, 3, 2]

temp = []

for x in ints_list:
    if x not in temp:
        temp.append(x)

ints_list = temp

print(f'Updated List after removing duplicates = {temp}')

Output: Updated List after removing duplicates = [1, 2, 3, 4]

输出Updated List after removing duplicates = [1, 2, 3, 4]

Recommended Reading: Python f-strings

推荐读物Python f字符串

2. set()函数 (2. set() function)

Python set doesn’t have duplicate elements. We can use the built-in set() function to convert the list to a set, then use the list() function to convert it back to the list.

Python集没有重复的元素。 我们可以使用内置的set()函数将列表转换为集合,然后使用list()函数将其转换回列表。

ints_list = [1, 2, 3, 4, 3, 2]

ints_list1 = list(set(ints_list))
print(ints_list1)  # [1, 2, 3, 4]

3.列出元素作为字典键 (3. List elements as Dictionary Keys)

We know that dictionary keys are unique. The dict class has fromkeys() function that accepts an iterable to create the dictionary with keys from the iterable.

我们知道字典键是唯一的。 dict类具有fromkeys()函数,该函数接受可迭代的对象,以使用可迭代对象的键来创建字典。

ints_list = [1, 2, 3, 4, 3, 2]

ints_list2 = list(dict.fromkeys(ints_list))
print(ints_list2)  # [1, 2, 3, 4]

4.列表count()函数 (4. List count() function)

The list count() method returns the number of occurrences of the value. We can use it with the remove() method to eliminate the duplicate elements from the list.

列表count()方法返回该值的出现次数。 我们可以将其与remove()方法一起使用,以从列表中消除重复的元素。

ints_list = [1, 2, 3, 4, 3, 2]

for x in ints_list:
    if ints_list.count(x) > 1:
        ints_list.remove(x)
print(ints_list)  # [1, 2, 3, 4]

5.清单理解 (5. List Comprehension)

We can create a list from an iterable using the list comprehension. This technique is the same as using the temporary list and the for loop to remove the duplicate elements. But, it reduces the number of lines of the code.

我们可以使用列表推导从可迭代对象创建列表。 此技术与使用临时列表和for循环删除重复的元素相同。 但是,它减少了代码的行数。

int_list = [1, 2, 3, 4, 3, 2]
temp = []
[temp.append(x) for x in ints_list if x not in temp]
print(temp)  # [1, 2, 3, 4]

从列表中删除重复项的最佳方法 (Best Way to Remove Duplicates from a List)

If you don’t want duplicate elements, you should use Set. But, if you have to remove the duplicate values from a list, then I would prefer count() function because it doesn’t create another temporary set or list object. So, it’s more memory efficient.

如果不想重复元素,则应使用Set。 但是,如果必须从列表中删除重复的值,则我更喜欢count()函数,因为它不会创建另一个临时集或列表对象。 因此,它具有更高的内存效率。

翻译自: https://www.journaldev.com/32742/python-remove-duplicates-from-list

python 删除重复项

  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值