Python根据值从字典列表中删除重复项

时间:2015-12-08 14:38:33

标签: python list dictionary

我有词典列表

vals = [
         {'tmpl_id': 67,  'qty_available': -3.0, 'product_id': 72, 'product_qty': 1.0},     
         {'tmpl_id': 67,  'qty_available': 5.0, 'product_id': 71, 'product_qty': 1.0}
         {'tmpl_id': 69,  'qty_available': 10.0, 'product_id': 74, 'product_qty': 1.0}
       ]

from operator import itemgetter
getvals = operator.itemgetter('tmpl_id')

val.sort(key=getvals)

result = []

for k, g in itertools.groupby(val, getvals):

    result.append(g.next())

val[:] = result

我想删除重复值(tmpl_id)并且还基于qty_available是较小或负面

输出将如下:

vals = [
          {'tmpl_id': 67,  'qty_available': 5.0, 'product_id': 71, 'product_qty': 1.0}
          {'tmpl_id': 69,  'qty_available': 10.0, 'product_id': 74, 'product_qty': 1.0}
       ]

2 个答案:

答案 0 :(得分:1)

您可以使用"tmpl_id"中的值作为键设置dict作为值来存储dicts,如果你得到一个更高'qty_available'的dict,那么你用当前的dict替换:

def remove_dupes(l, k, k2):
    seen = {} 
    for d in vals:
        v, v2 = d[k], d[k2]
        if v not in seen:
            seen[v] = d
        elif v2 > seen[v][k2]:
            seen[v] = d
    return seen

vals[:] = remove_dupes(vals, "tmpl_id",'qty_available' ).values()

输出:

[{'product_id': 71, 'qty_available': 5.0, 'tmpl_id': 67, 'product_qty': 1.0}, 
{'product_id': 74, 'qty_available': 10.0, 'tmpl_id': 69, 'product_qty': 1.0}]

如果你要使用sorted和groupby,你只需要反向排序并从每个v中获取第一个值:

from itertools import groupby
from operator import itemgetter

keys = itemgetter("tmpl_id",'qty_available')

vals[:] = (next(v) for k,v in groupby(sorted(vals, key=keys,reverse=True), 
                 key=itemgetter("tmpl_id")))

print(vals)

反转排序意味着较高的'qty_available'将首先出现,因此对于唯一的dicts,它只会给你那个dict,对于重复的tmpl_id,你将获得qty_available'的最大值的那个。

如果您想要一个就地排序而不是创建一个新列表,只需使用vals.sort()并删除对已排序的调用

答案 1 :(得分:0)

from collections import Counter

vals = [{'tmpl_id': 67,  'qty_available': -3.0, 'product_id': 72, 'product_qty': 1.0},
        {'tmpl_id': 67,  'qty_available': 5.0, 'product_id': 71, 'product_qty': 1.0},
        {'tmpl_id': 69,  'qty_available': 10.0, 'product_id': 74, 'product_qty': 1.0},]

k = [x['tmpl_id'] for x in vals]

new_vals=[]

for i in Counter(k):
    all = [x for x in vals if x['tmpl_id']==i]
    new_vals.append(max(all, key=lambda x: x['qty_available']))

>>> new_vals
[
    {'product_qty': 1.0, 'qty_available': 5.0, 'tmpl_id': 67, 'product_id': 71}, 
    {'product_qty': 1.0, 'qty_available': 10.0, 'tmpl_id': 69, 'product_id': 74}
]