我有兩列數據 (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 = {}以 –
開頭好吧。完成後,我仍然遇到同樣的錯誤。 –