简体   繁体   中英

Matching two lists to retrieve values from a third list in Python

I have two csv's who both have product-ID's and information on sales I want to structurally combine.

CSV 1: Product ID, Sale Yes/No

CSV 2: Product ID, Date of sale

Steps I have taken now is to:

(1) Make a list of all product ID's from CSV1 that are sold == yes

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

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

(2) Now I want to use these ID's to retrieve from CSV2 a list of salesdates that belong to these ID's

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"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM