我需要帮助查找使用most_common
函数后打印字符串最常见的字符字母作为角色。我的代码是:
from collections import*
message = input("What is the message you would like to decrypt?")
messageInt = list(map(ord,list(message)))
messageChr = list(map(chr,list(messageInt)))
print messageChr
fre = Counter(messageChr)
mostLett = fre.most_common(1)
print mostLett
我如何打印它:
['e', 'x', 'a', 'm', 'p', 'l', 'e']
[('e', 2)]
e
l = ['e', 'x', 'a', 'm', 'p', 'l', 'e']
Counter(l).most_common(1)[0][0]
or
Counter(l).most_common(1).pop()[0]
or
mostCommonLetter, _ = Counter(l).most_common(1).pop()
mostCommonLetter
'e'
带有python 2,这是我认为您正在使用的,
from collections import*
message = raw_input("What is the message you would like to decrypt?")
messageInt = list(map(ord,list(message)))
messageChr = list(map(chr,list(messageInt)))
print messageChr
fre = Counter(messageChr)
mostLett = fre.most_common(1)
print mostLett
print mostLett[0][0]
用raw_input()
替换input()
并添加底线。