通过乘参数过滤字典列表

时间:2020-02-02 13:06:15

标签: python

我有一个字典列表,这时我的代码检查product_id是否相同,然后只加1行并求和qty

问题是,它应该汇总成本相同的生产线的数量,如果成本不同,则应将其添加为另一行。尝试这样做6个小时没有运气,所以我在这里寻求帮助。

values = [
    {
        'status': u'Purchase',
        'phantom': False,
        'row_no': 1,
        'product_id': product.product(29793,),
        'standard_price': 29.8533333333333,
        'qty': 8.0,
        'cost': 4.78
    },
    {
        'status': u'Purchase',
        'phantom': False,
        'row_no': 2,
        'product_id': product.product(29793,),
        'standard_price': 29.8533333333333,
        'qty': 2.0,
        'cost': 80.0
    },
    {
        'status': u'Purchase',
        'phantom': False,
        'row_no': 3,
        'product_id': product.product(29793,),
        'standard_price': 29.8533333333333,
        'qty': 2.0,
        'cost': 80.0
    }
]

seen = set()
result = []
sum_res = {}

for dic in values:
    key = (dic['product_id'])
    if key in seen:
        for k, v in sum_res.iteritems():
            if k == key:
                sum_res[k] += dic['qty']
        continue
    sum_res[dic['product_id']] = dic['qty']
    result.append(dic)

    seen.add(key)

row = 0

for dict in result:
    row += 1
    for k, v in sum_res.iteritems():
        if k == dict['product_id']:
            dict['qty'] = v
            dict['row_no'] = row

# summed_results = []

for dic in result:
    dic['sum_cost'] = dic['qty'] * dic['cost']

我的输出:

result = [
    {
        'status': u'Purchase',
        'phantom': False, 
        'sum_cost': 57.36,
        'row_no': 1,
        'product_id': product.product(29793,),
        'standard_price': 29.8533333333333,
        'qty': 12.0,
        'cost': 4.78
    }
]

所需的输出:

result = [
    {
        'status': u'Purchase',
        'phantom': False,
        'sum_cost': 38.24,
        'row_no': 1,
        'product_id': product.product(29793,),
        'standard_price': 29.8533333333333,
        'qty': 8.0,
        'cost': 4.78
    },
    {
        'status': u'Purchase',
        'phantom': False,
        'sum_cost': 320,
        'row_no': 2,
        'product_id': product.product(29793,), 
        'standard_price': 29.8533333333333,
        'qty': 4.0,
        'cost': 80
    }
]

1 个答案:

答案 0 :(得分:2)

from collections import defaultdict

values = [
    {'status': u'Purchase', 'phantom': False, 'row_no': 1, 'product_id': 29793, 'standard_price': 29.8533333333333,
     'qty': 8.0, 'cost': 4.78},
    {'status': u'Purchase', 'phantom': False, 'row_no': 2, 'product_id':
        29793, 'standard_price': 29.8533333333333, 'qty': 2.0, 'cost': 80.0},
    {'status': u'Purchase', 'phantom': False, 'row_no': 3, 'product_id':
        29793, 'standard_price': 29.8533333333333, 'qty': 2.0, 'cost': 80.0}]

seen = set()
result = []
sum_res = defaultdict(int)
row = 0

for dic in values:
    key = (dic['product_id'], dic['cost'])
    sum_res[key] += dic['qty']

    if key not in seen:
        result.append(dic)
        seen.add(key)

for dic in result:
    key = (dic['product_id'], dic['cost'])
    row += 1
    dic['row_no'] = row
    dic['qty'] = sum_res[key]
    dic['sum_cost'] = dic['qty'] * dic['cost']

print(result)