python 数组列表去重

发表时间:2019-10-23 10:18:52
摘要信息

python 数组列表去重

class Dedupe:
def __init__(self):
self.data = None

def dedupe(self, 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)

使用:

hignList = list(
p_message.objects.all().values('m_pz', 'id', 'm_c_id').annotate(avg=Avg("m_c_id")).order_by('-num'))
deep = Dedupe()
hignList = list(deep.dedupe(hignList, key=lambda d: d['m_c_id']))


40% Complete (success)