Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用max函数查找列表列表中最长的字符串_Python_Max - Fatal编程技术网

Python 使用max函数查找列表列表中最长的字符串

Python 使用max函数查找列表列表中最长的字符串,python,max,Python,Max,我可以用列表理解来做这件事,没问题,但是仅仅用max怎么能工作呢 # This works a_list = ["a_string", "the_longest_string", "string"] longest_string = max(a_list, key=len) print(longest_string) # Now how to do it when the data structure is a list of list

我可以用列表理解来做这件事,没问题,但是仅仅用max怎么能工作呢

# This works
a_list = ["a_string", "the_longest_string", "string"]
longest_string = max(a_list, key=len)
print(longest_string)
# Now how to do it when the data structure is a list of lists
a_list = [["a_string"], ["the_longest_string"], ["string"]]
longest_string = max(a_list, key=len)
print(longest_string)
试试这个:

max(a_list, key=lambda x: len(*x))

你希望从这份名单中得到什么?字符串最长的列表?还是仅仅是最长的字符串本身?在这些内部列表中是否总是只有一个字符串?在第二种情况下,所有len都是1,因此max返回他想要的MapiterTools.chain.from_iterablea_list,key=len…?这个*的作用是什么?@nicomp尝试打印[a,b]和打印*[a,b],你会看到区别