这里是python的新手。 我想将重复的字典键消除为一个,同时执行算术,如添加/减去值,如果找到重复项。
当前代码输出
{(&#39; GRILLED AUSTRALIA ANGU&#39;,):((&#39; 1&#39;,),(&#39; 29.00&#39;,)),(&#39;啤酒&#39;,&#39;胡萝卜 Cake&#39;,&#39; Chocolate Cake&#39;):((&#39; 10&#39;,&#39; 1&#39;,&#39; 1&#39;),(&#39; ; 30.00&#39;,&#39; 2.50&#39;, &#39; 3.50&#39;)),(&#39; 啤酒&#39;,&#39; 啤酒&#39;):(( &#39; 1 &#39;,&#39; 1 &#39;),(&#39; 3.00 &# 39;,&#39; 3.00 &#39;)),&#39;胡萝卜 Cake&#39;,&#39; Chocolate Cake&#39;):((&#39; 1&#39;,&#39; 1&#39;),(&#39; 2.50&#39;,&# 39; 3.50&#39;)),&#39;胡萝卜 Cake&#39;,):((&#39; 1&#39;,),(&#39; 2.50&#39;,)),(&#39; BRAISED BEANCURD WITH&#39;,):(( #&39; 1&#39;,), (&#39; 10.00&#39;,)),(&#39;用B&#39;包裹的香肠,&#39; ESCARGOT与GARLIC H&#39;,&#39; PAN SEARED FOIE GRAS&#39;,&#39; SAUTE FIELD MUSHROOM W&#39;,&#39; CRISPY CHICKEN WINGS&#39;, &#39;洋葱圈&#39;):((&#39; 1&#39;,&#39; 1&#39;,&#39; 1&#39;,&#39; 1&#39;,& #39; 1&#39;,&#39; 1&#39;),(&#39; 10.00&#39;,&#39; 12.00&#39;, &#39; 15.00&#39;,&#39; 9.00&#39;,&#39; 7.00&#39;,&#39; 6.00&#39;)),(&#39; 啤酒&#39; <#39; 啤酒&#39;,&#39; 胡萝卜蛋糕&#39;, &#39; 巧克力蛋糕&#39;):((&#39; -1 &#39;,&#39; 10 &#39;,&#39; 1 &#39;,&#39; 1 &#39;),(&#39; - 3.00 &#39;,&#39; 30.00 &#39;,&#39; 2.50 &#39;, &#39; 3.50 &#39;)),(&#39;啤酒&#39;):((&#39; 10&#39;,),(&#39; 30.00 &#39;,))}
我想要的是什么:例如:
重复的减法
{&#39; Beer&#39;:[9,27]},{&#39;胡萝卜蛋糕&#39;:[1,2.5]},{&#39;巧克力蛋糕&#39;:[ 1, 3.5]}
请注意,对于重复项目输入,我将啤酒整理为一个(10-1 = 9),数量和(30-3 = 27)成本。我如何自动化这个过程?
重复添加
{&#39; Beer&#39;:[2,6]}
请注意,我将啤酒和啤酒添加到一个条目中,并且数量(1 + 1)和成本(3 + 3 = 6)
我的代码:
import csv
from itertools import groupby
from operator import itemgetter
import re
d = {}
#open directory and saving directory
with open("rofl.csv", "rb") as f, open("out.csv", "wb") as out:
reader = csv.reader(f)
next(reader)
writer = csv.writer(out)
#the first column header
writer.writerow(["item","quantity","amount"])
groups = groupby(csv.reader(f), key=itemgetter(0))
for k, v in groups:
v = list(v)
sales= [ x[1] for x in v[8:] ]
salesstring= str(sales)
#using re.findall instead of re.search to return all via regex for items
itemoutput= re.findall(r"(?<=\s\s)\w+(?:\s\w+)*(?=\s\s)",textwordfortransaction)
#using re.findall instead of re.search to return all via regex for amount aka quantity
amountoutput= re.findall(r"'(-?\d+)\s+(?:[A-Za-z ]*)",textwordfortransaction)
#using re.findall instead of re.search to return all via regex for cost
costoutput= re.findall(r"(?:'-?\d+[A-Za-z ]*)(-?\d+[.]?\d*)",textwordfortransaction)
d[tuple(itemoutput)] = tuple(amountoutput),tuple(costoutput)
#writing the DATA to output CSV
writer.writerow([d])
#to remove the last entry else it would keep on stacking the previous
d.clear()
如果需要,链接到csv文件 https://drive.google.com/open?id=0B1kSBxOGO4uJOFVZSWh2NWx6dHc
答案 0 :(得分:2)
使用问题中发布的当前输出,您只需zip
项目和数量和价格的不同列表,就可以将项目相互对齐,将它们添加到两个{{1}中},最后将结果与结果相结合。
defaultdicts
之后,output = {('GRILLED AUSTRALIA ANGU',): (('1',), ('29.00',)), ...}
from collections import defaultdict
prices, quantities = defaultdict(int), defaultdict(int)
for key, val in output.items():
for item, quant, price in zip(key, *val):
quantities[item] += int(quant)
prices[item] += float(price)
result = {item: (quantities[item], prices[item]) for item in prices}
是这样的:请注意,当数量和/或价格为负时,不需要特殊情况来减去重复项;只需添加负数。
result
如果您想将各个项目分开,只需在外部循环中移动{'ESCARGOT WITH GARLIC H': (1, 12.0),
'BRAISED BEANCURD WITH': (1, 10.0),
'CRISPY CHICKEN WINGS': (1, 7.0),
'SAUSAGE WRAPPED WITH B': (1, 10.0),
'ONION RINGS': (1, 6.0),
'PAN SEARED FOIE GRAS': (1, 15.0),
'Beer': (31, 93.0),
'Chocolate Cake': (3, 10.5),
'SAUTE FIELD MUSHROOM W': (1, 9.0),
'Carrot Cake': (4, 10.0),
'GRILLED AUSTRALIA ANGU': (1, 29.0)}
,prices
和quantities
的声明:
result
双啤酒系列的实例结果:
for key, val in output.items():
prices, quantities = defaultdict(int), defaultdict(int)
for item, quant, price in zip(key, *val):
quantities[item] += int(quant)
prices[item] += float(price)
result = {item: (quantities[item], prices[item]) for item in prices}
# do something with result or collect in a list
如果您希望('Beer', 'Beer', 'Carrot Cake', 'Chocolate Cake') (('-1', '10', '1', '1'), ('-3.00', '30.00', '2.50', '3.50'))
{'Chocolate Cake': (1, 3.5), 'Beer': (9, 27.0), 'Carrot Cake': (1, 2.5)}
将商品,数量和价格组合在一起,请使用:
result
结果是这样的:
items = list(prices)
result = (items, [quantities[x] for x in items], [prices[x] for x in items])