python列表如何去重_Python 列表去重怎么用暴力枚举法写出

16

2018-08-30 13:45:07 +08:00 heart_neue_red.png?v=16ec2dd0a880be6edda1e4a2e35754b3 1

楼主是否看过 python cookbook ?

If the values in the sequence are hashable, the problem can be easily solved using a set and a generator. For example:

def dedupe(items):

seen = set()

for item in items:

if item not in seen:

yield item seen.add(item)

Here is an example of how to use your function:

>>> a = [1, 5, 2, 1, 9, 1, 5, 10]

>>> list(dedupe(a))

[1, 5, 2, 9, 10]

>>>

This only works if the items in the sequence are hashable. If you are trying to eliminate duplicates in a sequence of unhashable types (such as dicts), you can make a slight change to this recipe, as follows:

def dedupe(items, key=None):

seen = set()

for item in items:

val = item if key is None else key(item)

if val not in seen:

yield item

seen.add(val)

Here, the purpose of the key argument is to specify a function that converts sequence items into a hashable type for the purposes of duplicate detection. Here ’ s how it works:

>>> a = [ {'x':1, 'y':2}, {'x':1, 'y':3}, {'x':1, 'y':2}, {'x':2, 'y':4}]

>>> list(dedupe(a, key=lambda d: (d['x'],d['y'])))

[{'x': 1, 'y': 2}, {'x': 1, 'y': 3}, {'x': 2, 'y': 4}]

>>> list(dedupe(a, key=lambda d: d['x']))

[{'x': 1, 'y': 2}, {'x': 2, 'y': 4}]

>>>

This latter solution also works nicely if you want to eliminate duplicates based on the value of a single field or attribute or a larger data structure.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值