试了试Pi,1000000位数,个数字的统计。
def load_data(path):
ret = ""#[]
for line in open (path,"r",encoding = "utf-8"):
ret += line.strip()
return ret
def statistic(strings):
t_char2cnt = {}
for char in strings:
if char not in t_char2cnt:
t_char2cnt[char] = 1
else:
t_char2cnt[char] +=1
return t_char2cnt
if __name__=="__main__":
path = "c:\\1.txt"
strings = load_data(path)
t_char2cnt = statistic(strings)
print(t_char2cnt)
结果如下:
'3': 100233, '1': 99762, '4': 100232, '5': 100364, '9': 100108, '2': 100032, '6': 99549, '8': 99990, '7': 99804, '0': 99970