,并先谢谢您的帮助。到目前为止,我已经有了这段代码:
with open("clean_result.csv", "r", encoding="utf-8", errors="ignore") as
new_data:
reader = csv.reader(new_data, delimiter=',', quotechar='"')
for row in reader:
if row:
columns = [row[0], row[1]]
init_dict.append(columns)
for ean, price in init_dict:
result[ean].append(price)
然后我在此行获得每个价格的最小值:
maxitems = {ean : min(result[ean]) for ean in result}
当前输出:{'8714789828558':'5,51','3326100000182':'15,00','3286010016683':'3,93'(...)}
我想添加row [2]并获取其他信息,但仅针对最低价格。
所需的输出:{'8714789828558':'5,51','A','3326100000182':'15,00','B''3286010016683':'3,93','C'(。 )}
我尝试过这个:
for row in reader:
if row:
columns = [row[0], row[1], row[2]]
init_dict.append(columns)
for ean, price, desc in init_dict:
result[ean].append(price)
result[ean].append(desc)
maxitems = {ean : min(result[ean]) for ean in result}
但是输出是这样的。缺少一半数据:
{'8714789828558':'A','3326100000182':'B''3286010016683':'C'(...)}
我可能误会了一些东西,所以请您给予任何帮助
答案 0 :(得分:1)
from operator import itemgetter
from collections import defaultdict
result = defaultdict(list)
for row in reader:
if row:
result[row[0]].append((row[1], row[2]))
minitems = {ean : min(prices, key = itemgetter(0)) for ean, prices in result.iteritems()}