gpt4 book ai didi

python - 在 Python 中匹配两个列表以从第三个列表中检索值

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:18 24 4
gpt4 key购买 nike

我有两个 csv,它们都有我想要结构性组合的产品 ID 和销售信息。

CSV 1: Product ID, Sale Yes/No

CSV 2: Product ID, Date of sale

我现在采取的步骤是:

(1) 列出 CSV1 中已售出的所有产品 ID == 是

f = open('oppcp.csv')
csv_f = csv.reader(f)
orderids = []

for row in csv_f:
if row[1] == "ORDERED":
orderids.append(row[2])

(2) 现在我想使用这些 ID 从 CSV2 检索属于这些 ID 的销售日期列表

最佳答案

import csv

sales_products = {row['Product ID']: row['Sale Yes/No'] for row in csv.DictReader(open('/path/to/sales_products.csv'))}
dates_products = {row['Product ID']: row['Date of sale'] for row in csv.DictReader(open('/path/to/dates_products.csv'))}
print {product_id: date_of_sale for product_id, date_of_sale in dates_products.items() if sales_products[product_id] == "Yes"}

关于python - 在 Python 中匹配两个列表以从第三个列表中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36115129/

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