如何打印列表中最常见的元素



我需要帮助查找使用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()并添加底线。

相关内容

  • 没有找到相关文章