数学建模例题2.11 首先生成包含1000个随机字符的字符串,然后统计每个字符出现次数,注意get()方法的应用

2.11.1
`
import string
import random
x=string.ascii_letters+string.digits
y=''.join([random.choice(x) for i in range(1000)])
d=dict()
for ch in y:
d[ch]=d.get(ch,0)+1;
for k,v in sorted(d.items()):
print(k,':',v)

print("学号:3005")
`

2.11.2
`import string,random,collections
x=string.ascii_letters+string.digits
y=''.join([random.choice(x) for i in range(1000)])
count=collections.Counter(y)
for k,v in sorted (count.items()):
print(k,':',v)

print("学号:3005")
`

posted on 2024-10-28 11:45  VVV1  阅读(2)  评论(0编辑  收藏  举报