随机产生1000个小写字母,统计出26个英文小写字母出现的次数,按照字母表顺序输出结果(提示:统计结果存储在字典里)

随机产生1000个小写字母,统计出26个英文小写字母出现的次数,按照字母表顺序输出结果(提示:统计结果存储在字典里)

import random
import collections
alpha = map(chr,random.choices(range(97,123),k=1000))
res = dict(collections.Counter(alpha))
for i,j in (sorted(res.items())):
    print(i,j)

from collections import Counter
import random
import string

alpha = [random.choice(string.ascii_lowercase) for i in range(1000)]
res = dict(sorted(Counter(alpha).items(), key = lambda x: x[0]))

for k, v in res.items():
    print(k, v)


#!/usr/bin/env python
# -*- coding:utf-8 -*-
import string
import random
from collections import Counter

if __name__ == '__main__':
    # x为包含所有小写字母的字符串
    x = string.ascii_lowercase
    y = []
    for i in range(1000):
        # 随机选取小写字母存入列表中
        y.append(random.choice(x))
    # 对列表中的元素进行计数,并将结果存入char_dict字典里
    char_dict = Counter(y)
    # 按照字母表顺序对字典进行排序
    char_dict = dict(sorted(char_dict.items(), key=lambda t: t[0]))
    # 输出结果
    for k, v in char_dict.items():
        print(k, v)

欢迎采纳,谢谢~

import random
import string

lst = []
dic = {}

while len(lst)<1000:
    lst.append( random.choice(string.ascii_lowercase) )

for s in lst:
    dic[s] = dic.get(s, 0) + 1

for key,value in (sorted(dic.items())):
    print(key,':',value)

a : 42
b : 27
c : 33
d : 38
e : 38
f : 41
g : 48
h : 30
i : 48
j : 37
k : 39
l : 49
m : 30
n : 41
o : 37
p : 39
q : 31
r : 38
s : 35
t : 35
u : 50
v : 32
w : 45
x : 42
y : 38
z : 37


import random

alpha_all = 'abcdefghijklmnopqrstuvwxyz'
statistics = {}
alpha_1000 = "".join(random.choices(alpha_all, k=1000))
for alpha in alpha_all:
    statistics[alpha] = alpha_1000.count(alpha)