python - 如何在 python 中返回最重复的字母?

标签 python

我已经做了一些挖掘并且大部分使用数组,但我们的类(class)并没有那么远,我们主要使用 for 循环来返回函数中重复次数最多的字母。

到目前为止,这是我的代码,但我所能得到的只是返回第一个字母的计数。

def most_repeated_letters(word_1):
  x = 0
  z = 0
  for letter in word_1:
    y = word_1.count(letter[0:])
    if y > z:
      z = y
    x += 1
    return z

print most_repeated_letters('jackaby')

最佳答案

利用collections.Counter

from collections import Counter
c = Counter('jackaby').most_common(1)
print(c)
# [('a', 2)]

关于python - 如何在 python 中返回最重复的字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46653281/

相关文章:

python - 格式不是字符串文字,也没有格式参数 [-Wformat-security]

python - python中的非典型舍入问题

python - 更改文件行 - Python

python - Windows 替代预期

python - Django RF 错误详细信息 : This field is required with APIClient

python - TensorFlow 数据集 .map() 方法不适用于内置 tf.keras.preprocessing.image 函数

python - 其余的 ironpython 异常在哪里?

python - 如何使用 NLTK 分词器去除标点符号?

python - 如何使用 Selenium 单击弹出模式框中的按钮

python - 如何在Python中将2D(m*n,m*n)矩阵分解为4D(m,m,n,n)矩阵?