gpt4 book ai didi

python - 组合两个 for 循环来填充同一个字典

转载 作者:太空宇宙 更新时间:2023-11-03 20:51:26 26 4
gpt4 key购买 nike

我试图从字典列表中获取两个不同的商家,优先考虑有价格的商家,如果没有找到两个不同的商家有价格,则商家 1 或 2 的价格将填充列表中的数据,如果列表不够,商家 1 或 2 应为“无”。

即for循环将返回两个商家,优先有价格的商家,如果不足以填充商家(1或2)则获取没有价格的商家。最后如果商家1或2仍未创建则填充它们无值。

这是我到目前为止的代码,它完成了工作,但我相信它可以以更 Pythonic 的方式组合。

import csv
with open('/home/timmy/testing/example/example/test.csv') as csvFile:
reader=csv.DictReader(csvFile)
for row in reader:
dummy_list.append(row)
item=dict()
index = 1
for merchant in dummy_list:
if merchant['price']:
if index==2:
if item['merchant_1']==merchant['name']:
continue
item['merchant_%d'%index] = merchant['name']
item['merchant_%d_price'%index] = merchant['price']
item['merchant_%d_stock'%index] = merchant['stock']
item['merchant_%d_link'%index] = merchant['link']
if index==3:
break
index+=1

for merchant in dummy_list:
if index==3:
break
if index<3:
try:
if item['merchant_1']==merchant['name']:
continue
except KeyError:
pass
item['merchant_%d'%index] = merchant['name']
item['merchant_%d_price'%index] = merchant['price']
item['merchant_%d_stock'%index] = merchant['stock']
item['merchant_%d_link'%index] = merchant['link']
index+=1

while index<3:
item['merchant_%d'%index] = ''
item['merchant_%d_price'%index] = ''
item['merchant_%d_stock'%index] = ''
item['merchant_%d_link'%index] = ''
index+=1
print(item)

这是 csv 文件的内容:

price,link,name,stock
,https://www.samsclub.com/sams/donut-shop-100-ct-k-cups/prod19381344.ip,Samsclub,
,https://www.costcobusinessdelivery.com/Green-Mountain-Original-Donut-Shop-Coffee%2C-Medium%2C-Keurig-K-Cup-Pods%2C-100-ct.product.100297848.html,Costcobusinessdelivery,
,https://www.costco.com/The-Original-Donut-Shop%2C-Medium-Roast%2C-K-Cup-Pods%2C-100-count.product.100381350.html,Costco,
57.99,https://www.target.com/p/the-original-donut-shop-regular-medium-roast-coffee-keurig-k-cup-pods-108ct/-/A-13649874,Target,Out of Stock
10.99,https://www.target.com/p/the-original-donut-shop-dark-roast-coffee-keurig-k-cup-pods-18ct/-/A-16185668,Target,In Stock
,https://www.homedepot.com/p/Keurig-Kcup-Pack-The-Original-Donut-Shop-Coffee-108-Count-110030/204077166,Homedepot,Undertermined

最佳答案

由于您只想保留最多 2 个商家,因此我只会处理一次 csv,分别保存有价格的商家列表和没有价格的商家列表,一旦找到 2 个有价格的商家就停止。

在该循环之后,我将连接这 2 个列表和两个空商家的列表,并获取其中的前 2 个元素。这足以保证您对 2 个不同商家的要求,并且优先于那些有价格的商家。最后,我将用它来填充 item 字典。

代码为:

import csv

with open('/home/timmy/testing/example/example/test.csv') as csvFile:
reader=csv.DictReader(csvFile)

names_price = set()
names_no_price = set()
merchant_price = []
merchant_no_price = []
item = {}

for merchant in reader:
if merchant['price']:
if not merchant['name'] in names_price:
names_price.add(merchant['name'])
merchant_price.append(merchant)
if len(merchant_price) == 2:
break;
else:
if not merchant['name'] in names_no_price:
names_no_price.add(merchant['name'])
merchant_no_price.append(merchant)

void = { k: '' for k in reader.fieldnames}
merchant_list = (merchant_price + merchant_no_price + [void, void.copy()])[:2]

for index, merchant in enumerate(merchant_list, 1):
item['merchant_%d'%index] = merchant['name']
item['merchant_%d_price'%index] = merchant['price']
item['merchant_%d_stock'%index] = merchant['stock']
item['merchant_%d_link'%index] = merchant['link']

关于python - 组合两个 for 循环来填充同一个字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com