随机生成英文字母,并且统计频数



import random
import collections
import string

#str1 = '赵钱孙李周吴郑王'
str1 = string.ascii_uppercase  # 大写 ABCDEFGHIJKLMNOPQRSTUVWXYZ
#str1 = string.ascii_lowercase # 小写 abcdefghijklmnopqrstuvwxyz
#str1 = string.ascii_letters   # 大写和小写 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
mylist = [random.choice(str1) for i in range(100)]
mycount = collections.Counter(mylist)
for key, val in mycount.most_common(10):  # 有序
    print(key, val

cmd中操作:
>>> import string
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> import random
>>> str1=string.ascii_uppercase
>>> str1
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> mylist = [random.choice(str1) for i in range(100)]
>>> mylist
['E', 'W', 'C', 'B', 'W', 'N', 'F', 'A', 'J', 'G', 'B', 'M', 'Z', 'U', 'D', 'Q',
 'F', 'C', 'Z', 'P', 'S', 'G', 'O', 'Q', 'P', 'W', 'N', 'K', 'Q', 'J', 'S', 'Z',
 'N', 'Q', 'Z', 'L', 'Z', 'E', 'G', 'G', 'D', 'N', 'F', 'X', 'K', 'H', 'M', 'S',
 'H', 'Z', 'N', 'N', 'R', 'H', 'Z', 'O', 'V', 'E', 'P', 'G', 'S', 'R', 'L', 'Z',
 'R', 'N', 'Z', 'D', 'G', 'D', 'Y', 'C', 'Y', 'M', 'B', 'P', 'K', 'C', 'R', 'D',
 'V', 'O', 'L', 'S', 'G', 'W', 'V', 'B', 'H', 'H', 'K', 'Z', 'I', 'B', 'O', 'S',
 'Q', 'V', 'P', 'W']
>>> import collections
>>> collections.Counter(mylist)
Counter({'Z': 10, 'G': 7, 'N': 7, 'S': 6, 'B': 5, 'D': 5, 'H': 5, 'Q': 5, 'P': 5
, 'W': 5, 'C': 4, 'K': 4, 'O': 4, 'R': 4, 'V': 4, 'E': 3, 'F': 3, 'M': 3, 'L': 3
, 'J': 2, 'Y': 2, 'A': 1, 'I': 1, 'U': 1, 'X': 1})
import random
import collections
import string

#str1 = '赵钱孙李周吴郑王'
str1 = string.ascii_uppercase  # 大写 ABCDEFGHIJKLMNOPQRSTUVWXYZ
#str1 = string.ascii_lowercase # 小写 abcdefghijklmnopqrstuvwxyz
#str1 = string.ascii_letters   # 大写和小写 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
mylist = [random.choice(str1) for i in range(100)]
mycount = collections.Counter(mylist)
for key, val in mycount.most_common(10):  # 有序
    print(key, val)
posted @ 2018-01-19 15:11  m*x*h  阅读(273)  评论(0编辑  收藏  举报