Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

python - List.append() seems to be rewriting values in list

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are creating a new list in each iteration which is causing the problem.

Try the following:

import csv
reader = csv.DictReader(open("storeLists.csv"))
storeAuniqueItems = []
storeBuniqueItems = []
storeCuniqueItems = []
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.append(row["NAME"])


    elif row["STORE A"] != "Y" and row["STORE B"] == "Y" and row["STORE C"] != "Y":
            storeBuniqueItems.append(row["NAME"])


    elif row["STORE A"] != "Y" and row["STORE B"] != "Y" and row["STORE C"] == "Y":
            storeCuniqueItems.append(row["NAME"])


    else:
            print("NaN")
print("STORE A:", storeAuniqueItems)
print("STORE B:", storeBuniqueItems)
print("STORE C:", storeCuniqueItems)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...