我需要能够存储数据,一个是数字,一个是它出现的次数。我有一个 for 循环,它调用一个返回字典的方法:
for x in range(total_holidays):
t = trial()
y = y + "\n" + str(x+1) + "," + str(t["brown"]) + "," + str(t["rainbow"]) + "," + str(t["nothing"]) + "," + str(t["days"])
total += t["days"]
#print total
if x%10000 == 0:
y0.append(y)
y = ""
基本上我需要计算 t['days'] 发生了多少次,这个数字几乎每次都在变化。如果你想要完整的代码看这里:
那么我该怎么做,然后我需要把它全部打印出来。
y
是 csv 文件的文本,total 用于计算平均值。
正如 mgilson 建议的那样,我应该使用它吗?
from collections import Counter
a = []
for x in range(total_holidays):
t = trial()
y = y + "\n" + str(x+1) + "," + str(t["brown"]) + "," + str(t["rainbow"]) + "," + str(t["nothing"]) + "," + str(t["days"])
total += t["days"]
a.append(t['days'])
#print total
if x%10000 == 0:
y0.append(y)
y = ""
z = Counter(a)
print z
我应该有类似的东西吗?