查找列表中的最大字符数:Python 代码

发布于 2024-12-27 18:54:17 字数 553 浏览 1 评论 0原文

我已经编写了以下代码,我需要从它给出的列表中获取最大字符数。

def ssin(a):
    ASAS = map(lambda y: (chr(ord('A')+y),len(filter(lambda x: ord(x) - ord('A') == y, a))),range(0,26))
    return ASAS

ssin('THE AGES OF THE KINGS')

答案:

[('A', 1), ('B', 0), ('C', 0), ('D', 0), ('E', 3), ('F', 1), ('G', 2),
('H', 2), ('I', 1), ('J', 0), ('K', 1), ('L', 0), ('M', 0), ('N', 1),
('O', 1), ('P', 0), ('Q', 0), ('R', 0), ('S', 2), ('T', 2), ('U', 0),
('V', 0), ('W', 0), ('X', 0), ('Y', 0), ('Z', 0)]

如何找到给定字符串中的最大字符数?

i have written the bellow code and i need to get the MAX number of characters from the list it gives.

def ssin(a):
    ASAS = map(lambda y: (chr(ord('A')+y),len(filter(lambda x: ord(x) - ord('A') == y, a))),range(0,26))
    return ASAS

ssin('THE AGES OF THE KINGS')

answer:

[('A', 1), ('B', 0), ('C', 0), ('D', 0), ('E', 3), ('F', 1), ('G', 2),
('H', 2), ('I', 1), ('J', 0), ('K', 1), ('L', 0), ('M', 0), ('N', 1),
('O', 1), ('P', 0), ('Q', 0), ('R', 0), ('S', 2), ('T', 2), ('U', 0),
('V', 0), ('W', 0), ('X', 0), ('Y', 0), ('Z', 0)]

how to find the MAX number of characters in a given string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花随流水 2025-01-03 18:54:17

Counter 是你的朋友:

>>> from collections import Counter
>>> Counter(char.upper() for char in 'THE AGES OF THE KINGS' if char.isalpha()).most_common(1)
[('E', 3)]

Counter is your friend:

>>> from collections import Counter
>>> Counter(char.upper() for char in 'THE AGES OF THE KINGS' if char.isalpha()).most_common(1)
[('E', 3)]
故事↓在人 2025-01-03 18:54:17

您的函数看起来很痛苦,但是如果您可以使用它来查找最频繁的值:

sorted(ssin('THE AGES OF THE KINGS'), key = itemgetter(1), reverse = True)[0]

这不会做任何事情来检查最频繁的值是否存在联系。

Your function is painful to the eye, but if you can use this to find the most frequent value:

sorted(ssin('THE AGES OF THE KINGS'), key = itemgetter(1), reverse = True)[0]

That doesn't do anything to check whether or not there are ties for the most frequent.

梦晓ヶ微光ヅ倾城 2025-01-03 18:54:17
>>> import operator
>>> freqinfo = ssin('THE AGES OF THE KINGS')
>>> max(freqinfo,key=operator.itemgetter(1))
('E',3)

基本上 max(freqinfo,key=operator.itemgetter(1)) 表示“在 freqinfo 中查找 freqinfo[i][1] 最大的对象”。因为 freqinfo[i] 是一个 (letter,Frequency) 对。

>>> import operator
>>> freqinfo = ssin('THE AGES OF THE KINGS')
>>> max(freqinfo,key=operator.itemgetter(1))
('E',3)

Basically max(freqinfo,key=operator.itemgetter(1)) says "look for the object in freqinfo for which the freqinfo[i][1] is maximum". Because freqinfo[i] is a (letter,frequency) pair.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文