Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 一本字典的产品不能重复_Python 3.x_Odoo - Fatal编程技术网

Python 3.x 一本字典的产品不能重复

Python 3.x 一本字典的产品不能重复,python-3.x,odoo,Python 3.x,Odoo,我正在拍摄当月销售的所有产品,并将其存储在字典列表中,如下所示: a = [ {'product_name':'coca-cola','qty':2}, {'product_name':'pepsi','qty':2}, {'product_name':'coca-cola','qty':1}, {'product_name':'coca-cola','qty':1}, {'product_name':'coca-cola1','qty':1},

我正在拍摄当月销售的所有产品,并将其存储在字典列表中,如下所示:

a = [
    {'product_name':'coca-cola','qty':2},
    {'product_name':'pepsi','qty':2},
    {'product_name':'coca-cola','qty':1},
    {'product_name':'coca-cola','qty':1},
    {'product_name':'coca-cola1','qty':1},
    {'product_name':'pepsi','qty':2}
    ]
现在我试着让产品不重复,坦率地说,如果你重复产品,你应该简单地添加离开字典的金额,如下所示:

result =[{'product_name':'coca-cola','qty':4},{'product_id':'pepsi','qty':4},{'product_id':'coca-colca1','qty':1}]
到目前为止,我只尝试了以下几点,但结果并不合适:

b = []

for index  in range(0,len(a)-1):
    if  a[index]['product_name'] != a[index + 1 ]['product_name']:
        b.append(a[index])


print b

尝试下面的方法

a = [
    {'product_name':'coca-cola','qty':2},
    {'product_name':'pepsi','qty':2},
    {'product_name':'coca-cola','qty':1},
    {'product_name':'coca-cola','qty':1},
    {'product_name':'coca-cola1','qty':1},
    {'product_name':'pepsi','qty':2}
    ]

b = []

for i in range(len(a)):
    if a[i]['product_name'] not in b : b.append(a[i]['product_name'])

for j in range(len(b)):
    qty = 0
    for k in range(len(a)):
        if a[k]['product_name'] == b[j] : qty += a[k]['qty']
    b[j] = {'product_name':b[j], 'qty':qty}

print(b)
我首先循环并将唯一的产品名称作为字符串输入到用b初始化的列表中。然后我循环遍历列表b中每个唯一的产品名称,然后总结与列表a中该产品名称相关的数量。最后,我将字符串条目转换为一个字典,其中包含现有的产品名称和总数量

输出

b = [
    {'product_name': 'coca-cola', 'qty': 4},
    {'product_name': 'pepsi', 'qty': 4},
    {'product_name': 'coca-cola1', 'qty': 1}
    ]

我的方法是使用两个步骤:首先,获取总数,然后以您需要的格式生成输出



    a = [
        {'product_name':'coca-cola','qty':2},
        {'product_name':'pepsi','qty':2},
        {'product_name':'coca-cola','qty':1},
        {'product_name':'coca-cola','qty':1},
        {'product_name':'coca-cola1','qty':1},
        {'product_name':'pepsi','qty':2}
        ]

    totals = {}
    for sale in a:
        if sale['product_name'] in totals.keys():
            totals[sale['product_name']] += sale['qty']
        else:
            totals[sale['product_name']] = sale['qty']

    b = []
    for product_name, qty in totals.items():
        b.append({'product_name':product_name, 'qty':qty})


这是解决方案我很惊讶你接受了这个答案。更多的代码行和更复杂的运行和逻辑。我认为左边的两个答案比这个好得多!!!!!