我有一个长度可变的列表,并尝试找到一种方法来测试当前正在评估的列表项是否为列表中包含的最长字符串。我正在使用Python 2.6.1。
例如:
例如:
mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something()
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else()
肯定有一种简短优雅的列表推导式我忽略了吧?
print(max(["this", "does", "work"], key=len))
只会返回"this"
而不是返回所有最长的字符串。 - Anderson Greenm=max(map(len,xs)); [x for x in xs if len(x) == m]
。我认为这不能很好地在一行中完成。 - Thomas Ahle