如何获得所有最大值max函数 [英] How to get all the maximums max function

查看:85
本文介绍了如何获得所有最大值max函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

max('str','frt',key=len)

max函数仅返回一个字符串.
如何使它返回两个字符串?
两个字符串的长度相等,因此max应该返回两个字符串,但是它只返回一个字符串,因此有办法返回所有max项.

The max function returns only one of the string.
How can i make it return both of the strings?
The length of both strings is equal so max should return both the strings butit return only one so is there a way to return all max items.

推荐答案

您可以将其写为列表理解:

You can write this as a list comprehension:

data = ['str', 'frt']
maxlen = max(map(len, data))
result = [s for s in data if len(s) == maxlen]

这篇关于如何获得所有最大值max函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆