python 句子中字母出现的次数
发布网友
发布时间:2022-04-23 15:40
我来回答
共1个回答
热心网友
时间:2023-10-08 18:51
第一题:
import re
import string
inText = raw_input('Please enter a string of text (the bigger the better): ')
outText = inText.lower()
outText = re.sub(r"[^a-z]","", outText)
count, letter = [], []
for l in string.ascii_lowercase:
n = outText.count(l)
if n != 0:
count.append(n)
letter.append(l)
map = sorted(zip(count, letter), key=lambda x:(x[0]), reverse=True)
print 'The distribution of characters in "' + inText + '" is:'
for c in map:
print c[1] * c[0]