我有两列数据 (sample data),我想计算每周的一天的总用户数。我的代码不会产生任何输出 - Python
举例来说,我想这样我的输出(字典/列出什么都行):
星期一:25,周二 :30,周三 :45,周四 :50,周五 : 24,周六 :22,周日 :21
这里是我的尝试:
def rider_ship (filename):
with open('./data/Washington-2016-Summary.csv','r') as f_in:
Sdict = []
Cdict = []
reader = csv.DictReader(f_in)
for row in reader:
if row['user_type']=="Subscriber":
if row['day_of_week'] in Sdict:
Sdict[row['day_of_week']]+=1
else:
Sdict [row['day_of_week']] = row['day_of_week']
else:
if row ['day_of_week'] in Cdict:
Cdict[row['day_of_week']] +=1
else:
Cdict[row['day_of_week']] = row['day_of_week']
return Sdict, Cdict
print (Sdict)
print (Cdict)
t= rider_ship ('./data/Washington-2016-Summary.csv')
print (t)
类型错误::列表索引必须为整数或切片,而不是str
欢迎#1,请阅读[如何提问](https://stackoverflow.com/help/how-to-ask)。请特别注意[如何创建MCVE](https://stackoverflow.com/help/mcve)。您将付出更多努力发布一个好问题:一个容易阅读,理解和[主题](https://stackoverflow.com/help/on-topic) - 的机会更高会吸引相关人员,你会得到更快的帮助。祝你好运! – alfasin
Sdict = {} Cdict = {}以 –
开头好吧。完成后,我仍然遇到同样的错误。 –