在 Python 中的列表列表中查找最长的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30902558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding the longest list in a list of lists in Python
提问by Liis Krevald
I have to fing the longest list of lists in Python.
我必须找到 Python 中最长的列表列表。
For example:
例如:
longest([1,2,3])
returns 3
longest([1,2,3])
返回 3
longest([[[1,2,3]]])
also returns 3 (inner list is 3)
longest([[[1,2,3]]])
也返回 3(内部列表为 3)
longest([[], [3,[4,5],[2,3,4,5,3,3], [7], 5, [1,2,3], [3,4]], [1,2,3,4,5]])
returns 7 (list [3,[4,5],[2,3,4,5,3,3], [7], 5, [1,2,3], [3,4]]
contains 7 elements)
longest([[], [3,[4,5],[2,3,4,5,3,3], [7], 5, [1,2,3], [3,4]], [1,2,3,4,5]])
返回 7(列表[3,[4,5],[2,3,4,5,3,3], [7], 5, [1,2,3], [3,4]]
包含 7 个元素)
Right now I have this code, but it doesn't do the trick with the first two examples..
现在我有这个代码,但它在前两个例子中不起作用。
def longest(list1):
longest_list = max(len(elem) for elem in list1)
return longest_list
Maybe recursion will help? Thank you!
也许递归会有所帮助?谢谢!
采纳答案by cr1msonB1ade
Here is a recursive solution for any depth list:
这是任何深度列表的递归解决方案:
def longest(l):
if(not isinstance(l, list)): return(0)
return(max([len(l),] + [len(subl) for subl in l if isinstance(subl, list)] +
[longest(subl) for subl in l]))
回答by fferri
Python 3.3 version:
Python 3.3 版本:
def lengths(x):
if isinstance(x,list):
yield len(x)
for y in x:
yield from lengths(y)
usage:
用法:
>>> l = [[], [3,[4,5],[2,3,4,5,3,3], [7], 5, [1,2,3], [3,4]], [1,2,3,4,5]]
>>> max(lengths(l))
7
In python 2.6+ you don't have the yield from
statement (was introduced in python 3.3), so you have to change the code slightly:
在 python 2.6+ 中,您没有该yield from
语句(在 python 3.3 中引入),因此您必须稍微更改代码:
def lengths(x):
if isinstance(x,list):
yield len(x)
for y in x:
for z in lengths(y):
yield z
回答by wvdz
Indeed, recursion can solve this.
确实,递归可以解决这个问题。
def longest(lst):
if type(lst) is not list:
return 0
max = len(lst)
for i in lst:
max_i = longest(i)
if max_i > max:
max = max_i
return max
回答by Willem Van Onsem
You can do this with recursion:
你可以用递归来做到这一点:
def longest(list1) :
l = 0
if type(list1) is list :
l = len(list1)
if l > 0 :
l = max(l,max(longest(elem) for elem in list1))
return l
The code first checks if this is list
we are dealing with. If so, we first take the len
of the list. Next we perform a recursive call on its elements. And calculate the maximum longest
of the elements. If the maximum is greater than the length itself. We return that maximum, otherwise we return the length.
代码首先检查is list
我们是否正在处理这个。如果是这样,我们首先取len
列表中的 。接下来我们对其元素执行递归调用。并计算longest
元素的最大值。如果最大值大于长度本身。我们返回最大值,否则返回长度。
Because the longest
of a non-list is zero, the recursion will stop, and we have an answer for single elements to be used in the inductive step.
因为longest
非列表的 the 为零,递归将停止,我们有一个在归纳步骤中使用的单个元素的答案。
回答by DTing
Another recursive function using map:
另一个使用 map 的递归函数:
def longest(a):
return max(len(a), *map(longest, a)) if isinstance(a, list) and a else 0
In [2]: longest([1,2,3])
Out[2]: 3
In [3]: longest([[[1,2,3]]])
Out[3]: 3
In [4]: longest([[], [3,[4,5],[2,3,4,5,3,3], [7], 5, [1,2,3], [3,4]], [1,2,3,4,5]])
Out[4]: 7
and iteratively:
并迭代地:
def longest(a):
mx = 0
stack = [a[:]]
while stack:
cur = stack.pop()
if isinstance(cur, list):
mx = max(mx, len(cur))
stack += cur
return mx
In [6]: longest([1,2,3])
Out[6]: 3
In [7]: longest([[[1,2,3]]])
Out[7]: 3
In [8]: longest([[], [3,[4,5],[2,3,4,5,3,3], [7], 5, [1,2,3], [3,4]], [1,2,3,4,5]])
Out[8]: 7
回答by FeiLiao
These simple few lines works for me, my list is a nested one (list of lists)
这些简单的几行对我有用,我的列表是嵌套的(列表列表)
#define the function#
def find_max_list(list):
list_len = [len(i) for i in list]
print(max(list_len))
#print output#
find_max_list(your_list)
回答by Mathias Wedeken
Using the toolz library, this can be achieved like this:
使用toolz 库,可以这样实现:
from toolz.curried import count
def longest(your_list):
return max(map(count, your_list))
One caveat: This doesn't work if your_list
contains non-iterables.
一个警告:如果your_list
包含不可迭代对象,这将不起作用 。
回答by binaryEcon
You can make use of enumerate
, sorted
and behavior of list
:
您可以使用enumerate
,sorted
和 行为list
:
ll = [[10,20], [1,2,3,4,5], [7,8,9]]
longest = ll[sorted([(i,len(l)) for i,l in enumerate(ll)], key=lambda t: t[1])[-1][0]]
>>longest
>>[1,2,3,4,5]
Explanation
解释
Create
list
oftuple
with index as first element andlen(l)
(length of list) as second element:[(i,len(l)) for i,l in enumerate(ll)]
>>[(0, 2), (1, 5), (2, 3)]
Sort above list by second element in tuple that tuple with longest length gets to the end of the list:
sorted([(i,len(l)) for i,l in enumerate(ll)], key=lambda t: t[1])
>>[(0, 2), (2, 3), (1, 5)]
Catch the last tuple and its first element:
sorted([(i,len(l)) for i,l in enumerate(ll)], key=lambda t: t[1])[-1][0]
>>1
Use above result as the index in
ll
to get the longest list:ll[sorted([(i,len(l)) for i,l in enumerate(ll)], key=lambda t: t[1])[-1][0]]
>>[1,2,3,4,5]
创建
list
的tuple
索引作为第一元件和len(l)
(列表的长度)为第二元件:[(i,len(l)) for i,l in enumerate(ll)]
>>[(0, 2), (1, 5), (2, 3)]
按元组中的第二个元素在列表上方排序,长度最长的元组到达列表的末尾:
sorted([(i,len(l)) for i,l in enumerate(ll)], key=lambda t: t[1])
>>[(0, 2), (2, 3), (1, 5)]
捕获最后一个元组及其第一个元素:
sorted([(i,len(l)) for i,l in enumerate(ll)], key=lambda t: t[1])[-1][0]
>>1
使用上面的结果作为索引
ll
来获得最长的列表:ll[sorted([(i,len(l)) for i,l in enumerate(ll)], key=lambda t: t[1])[-1][0]]
>>[1,2,3,4,5]