I am impoting a CSV file, where there a three shops (shop A, B and C). There are rows of items, and there are values in the cells of the rows, indicating whether the shop sells the item or not.
Im attempting to go through each row, and my goal is to find items that are only sold by one shop (an item unique to the shop)
If there is an item that is unique to the shop, I want to append the item to a list (I have one for each shop).
my problem is that when I print all the items with my conditions, it shows me all the items that are unique to a shop, however when I append them to my list and print my list, only the last item shows (which is why i think it is being overwritten).
import csv
reader = csv.DictReader(open("storeLists.csv"))
for row in reader:
STORES = [row]
"COMPARING ALL THE SHOPS TO GATHER ALL THE UNIQUE ITEMS OF EACH SHOP AND SAVE THE INTO A LIST"
if row["STORE A"] == "Y" and row["STORE B"] != "Y" and row["STORE C"] != "Y":
storeAuniqueItems = []
storeAuniqueItems.append(row["NAME"])
elif row["STORE A"] != "Y" and row["STORE B"] == "Y" and row["STORE C"] != "Y":
storeBuniqueItems = []
storeBuniqueItems.append(row["NAME"])
elif row["STORE A"] != "Y" and row["STORE B"] != "Y" and row["STORE C"] == "Y":
storeCuniqueItems = []
storeCuniqueItems.append(row["NAME"])
else:
print("NaN")
print("STORE A:", storeAuniqueItems)
print("STORE B:", storeBuniqueItems)
print("STORE C:", storeCuniqueItems)
question from:
https://stackoverflow.com/questions/65936031/list-append-seems-to-be-rewriting-values-in-list 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…