并先感谢您的帮助。到目前为止,我有此代码:

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'(...)}

我可能误会了一些东西,所以请任何帮助表示赞赏

最佳答案

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()}

关于python - 默认字典/按分钟排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53009563/

10-12 16:38