Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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 查找嵌套列表中列表的最大值_Python_List_Nested_Max - Fatal编程技术网

Python 查找嵌套列表中列表的最大值

Python 查找嵌套列表中列表的最大值,python,list,nested,max,Python,List,Nested,Max,我想把每个列表的所有max元素放在b到c中。 但我一直在获取整个列表的最大元素,而我想 嵌套列表中每个列表的最大值,即[45,56]您可以使用为每个子列表取最大值的al: 2 1 3 45 6 8 2 4 56 7 [[1, 3, 45, 6, 8], [2, 4, 56, 7]] [1, 3, 45, 6, 8] [56, 56, 56] 输出 b = [[1, 3, 45, 6, 8], [2, 4, 56, 7]] c = [max(l) for l in b] print(c)

我想把每个列表的所有max元素放在b到c中。 但我一直在获取整个列表的最大元素,而我想 嵌套列表中每个列表的最大值,即[45,56]

您可以使用为每个子列表取最大值的a
l

2
1 3 45 6 8 
2 4 56 7 
[[1, 3, 45, 6, 8], [2, 4, 56, 7]]
[1, 3, 45, 6, 8]
[56, 56, 56]

输出

b = [[1, 3, 45, 6, 8], [2, 4, 56, 7]]
c = [max(l) for l in b]

print(c)
上述列表理解等同于以下for循环:

[45, 56]
您可以使用为每个子列表取最大值的:

2
1 3 45 6 8 
2 4 56 7 
[[1, 3, 45, 6, 8], [2, 4, 56, 7]]
[1, 3, 45, 6, 8]
[56, 56, 56]

输出

b = [[1, 3, 45, 6, 8], [2, 4, 56, 7]]
c = [max(l) for l in b]

print(c)
上述列表理解等同于以下for循环:

[45, 56]

您有一个二维列表,并试图返回该二维列表中每个元素的最大值列表。迭代2D列表并获取每个元素的最大值:

c = []
for l in b:
    c.append(max(l))
此外,您还可以使用
映射

res = [max(i) for i in nested_list]

您有一个二维列表,并试图返回该二维列表中每个元素的最大值列表。迭代2D列表并获取每个元素的最大值:

c = []
for l in b:
    c.append(max(l))
此外,您还可以使用
映射

res = [max(i) for i in nested_list]

您还可以将嵌套列表转换为
Pandas Dataframe
,并使用
max
函数。 那么你就不必担心循环了。

res = list(map(max, nested_list))

您还可以将嵌套列表转换为
Pandas Dataframe
,并使用
max
函数。 那么你就不必担心循环了。

res = list(map(max, nested_list))