在python中查找对象列表中的键出现

时间:2017-10-26 11:27:13

标签: python list

我正在尝试转换此列表:

list =  [
            {'product_name': '4x6', 'quantity': 1, 'price': 0.29},
            {'product_name': '4x6', 'quantity': 1, 'price': 0.29},
            {'product_name': '4x6', 'quantity': 1, 'price': 0.29},
            {'product_name': '4x6', 'quantity': 1, 'price': 0.29},
            {'product_name': '4x4', 'quantity': 1, 'price': 0.29},
            {'product_name': '4x4', 'quantity': 1, 'price': 0.29},
            {'product_name': '4x4', 'quantity': 1, 'price': 0.29},
            {'product_name': '4x4', 'quantity': 1, 'price': 0.29},
        ]

成为关键'product_name'的出现总和,如下所示:

list_final =  [
            {'product_name': '4x6', 'quantity': 4, 'price': 1.16},
            {'product_name': '4x4', 'quantity': 4, 'price': 1.16},
        ]

如果不在循环中执行循环,我无法想象如何搜索键'product_name'的出现 我做了什么:

for item in list:
    if item.product_name in data.keys():
        data[item.product_name]['qty'] += 1
        data[item.product_name]['price'] *= 2
    else:
        data.update({item.product_name: [{'qty': item['quantity'], 'price': item['price']}]})

但我无法找到解决方案来获取我想要的清单

我该怎么做?

3 个答案:

答案 0 :(得分:3)

这是一个处理多个产品的OrderedDict解决方案。

from collections import OrderedDict

o = OrderedDict()    
for x in data:
    p = x['product_name']
    if p not in o:
        o[p] = x
    else:
        o[p].update({k : o[p][k] + x[k] for k in x.keys() - {'product_name'}})

list_final = list(o.values())

如果产品不存在,则会将产品添加到库存中,或者将产品与现有库存相加。这应该适用于python3.x及更高版本。

print(list_final)
[{'price': 1.16, 'product_name': '4x6', 'quantity': 4}]

对于python2.x,请更改此

o[p].update({k : o[p][k] + x[k] for k in x.keys() - {'product_name'}})

o[p].update({k : o[p][k] + x[k] for k in set(x.keys()) - {'product_name'}})

答案 1 :(得分:1)

可能不是最具可读性,但这是一个无循环实现:

smart banner

然后你就

def transform(array):
    def inner(cumulator, row):
        product_name = row['product_name']
        bucket = cumulator.get(product_name, {'quantity': 0, 'price': 0})
        cumulator[product_name] = {
            'quantity': bucket['quantity'] + row['quantity'],
            'price': bucket['price'] + row['price'],
        }
        return cumulator
    return reduce(inner, array, {})

答案 2 :(得分:0)

这可能会有所帮助:

l =  [
        {'product_name': '4x6', 'quantity': 1, 'price': 0.29},
        {'product_name': '4x6', 'quantity': 1, 'price': 0.29},
        {'product_name': '4x6', 'quantity': 1, 'price': 0.29},
        {'product_name': '4x6', 'quantity': 1, 'price': 0.29},
        {'product_name': '4x4', 'quantity': 1, 'price': 0.29},
        {'product_name': '4x4', 'quantity': 1, 'price': 0.29},
        {'product_name': '4x4', 'quantity': 1, 'price': 0.29},
        {'product_name': '4x4', 'quantity': 1, 'price': 0.29},
    ]

from collections import defaultdict
count = defaultdict(lambda: {'quantity':0, 'price':0.0})

for d in l:
  count[d['product_name']]['quantity'] += 1
  count[d['product_name']]['price'] = d['price']

for prod_name, prod_info in count.items():
  print("product_name:", prod_name, "quantity: {quantity} price: {price}".format(**prod_info))

输入的输出:

product_name: 4x6 quantity: 4 price: 0.29
product_name: 4x4 quantity: 4 price: 0.29

注意:这也适用于python2