查找列表中的最大字符数:Python 代码
我已经编写了以下代码,我需要从它给出的列表中获取最大字符数。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Counter
是你的朋友:Counter
is your friend:您的函数看起来很痛苦,但是如果您可以使用它来查找最频繁的值:
这不会做任何事情来检查最频繁的值是否存在联系。
Your function is painful to the eye, but if you can use this to find the most frequent value:
That doesn't do anything to check whether or not there are ties for the most frequent.
基本上
max(freqinfo,key=operator.itemgetter(1))
表示“在freqinfo
中查找 freqinfo[i][1] 最大的对象”。因为freqinfo[i]
是一个(letter,Frequency)
对。Basically
max(freqinfo,key=operator.itemgetter(1))
says "look for the object infreqinfo
for which the freqinfo[i][1] is maximum". Becausefreqinfo[i]
is a(letter,frequency)
pair.