我对编程比较陌生,一直在努力学习python。我有一个问题,我花了几个小时,似乎无法解决如何解决它。
我有一个CSV文件,其中包含我想循环访问数据的一些数据,并创建一个字典,其中键是人,值是字典,其中键是一个项目,值是该项目的数量他们有。我还想排除一些我不在乎的数据。在这个例子中,我要说的是我不关心他们有多少菠萝。
示例CSV数据:
Name Fruit Bob, Apples Bob, Pineapple Bob, Apples Bob, Oranges Bob, Oranges Bob, Oranges Bob, Kiwi Kate, Oranges Kate, Pineapple Kate, Oranges Kate, Apples Mary, Kiwi Mary, Pineapple
预期结果= {Bob: {Apples:2, Oranges:3, Kiwi:1} Kate:{Oranges:2, Apple:1} Mary:{Kiwi:1}}
这是我到目前为止所提出的,但是当它到达第二个人时它运行时计数已关闭,因为它不会在每个用户之后重置计数。
import csv
from collections import Counter
fruitToAnalyze = ['Apple','Oranges','Kiwi']
final = {}
fruitlist = []
with open("data.csv") as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
next(csvfile)
for row in readCSV:
person = row[0]
fruit = row[1]
if fruit in fruitToAnalyze:
fruitlist.append(fruit)
countedfruitlist = Counter(fruitlist)
final[person] = countedfruitlist
我似乎无法找到一种方法来重置每个人的计数器以及第一个人错误后所有人的计数,因为它添加它是计算整个列表而不是每个人人
实际结果:
{Bob: {Apples:2, Oranges:3, Kiwi:1} Kate:{Oranges:5, Apple:3} Mary:{Kiwi:2, Oranges:5, Apple:3}}
我可能不会以正确的方式解决这个问题。打开如何解决这个或更好的方法来做到这一点
答案 0 :(得分:0)
如果您可以假设CSV文件按名称排序(如您给出的示例中所示),则每次遇到新名称时都可以重置fruitlist
:
prevPerson = ''
for row in readCSV:
person = row[0]
fruit = row[1]
if (prevPerson != person):
fruitlist = []
prevPerson = person
if fruit in fruitToAnalyze:
fruitlist.append(fruit)
countedfruitlist = Counter(fruitlist)
final[person] = countedfruitlist
否则,您为每个人保留一个fruitlist
数组,并且只有在完成后,才将其转为计数器。
for row in readCSV:
person = row[0]
fruit = row[1]
if fruit in fruitToAnalyze:
if (person not in final):
final[person] = []
final[person].append(fruit)
... # finish code
for person in final.keys():
final[person] = Counter(final[person])
答案 1 :(得分:0)
我不确定你是否真的需要一个柜台。您只需初始化默认字典
即可注意:此方法不会假设人们没有在您的文件中混插
import csv
fruitToAnalyze = {'Apples','Oranges','Kiwi'}
final = {}
with open("data.csv") as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
next(csvfile)
for row in readCSV:
person = row[0]
# Initializes only one person dict
if person not in final:
final[person] = {f: 0 for f in fruitToAnalyze}
fruit_counter = final[person]
fruit = row[1].strip()
if fruit in fruitToAnalyze:
fruit_counter[fruit] += 1
输出{'Bob': {'Oranges': 3, 'Kiwi': 1, 'Apples': 2}, 'Kate': {'Oranges': 2, 'Kiwi': 0, 'Apples': 1}, 'Mary': {'Oranges': 0, 'Kiwi': 1, 'Apples': 0}}