在Python中的每个子列表中查找最大值
我们得到一个列表列表。在内部列表或子列表中,我们需要在每个列表中找到最大值。
与最大和
我们设计一个带in条件的for循环,并应用max函数来获取每个子列表中的最大值。
示例
Alist = [[10, 13, 454, 66, 44], [10, 8, 7, 23]]
# Given list
print("The given list:\n ",Alist)
# Use Max
res = [max(elem) for elem in Alist]
# Printing max
print("Maximum values from each element in the list:\n ",res)输出结果
运行上面的代码给我们以下结果-
The given list: [[10, 13, 454, 66, 44], [10, 8, 7, 23]] Maximum values from each element in the list: [454, 23]
带映射和最大
在遍历子列表时,我们继续使用map来应用max函数。
示例
Alist = [[10, 13, 454, 66, 44], [10, 8, 7, 23]]
# Given list
print("The given list:\n ",Alist)
# Use Max
res =list(map(max, Alist))
# Printing max
print("Maximum values from each element in the list:\n ",res)输出结果
运行上面的代码给我们以下结果-
The given list: [[10, 13, 454, 66, 44], [10, 8, 7, 23]] Maximum values from each element in the list: [454, 23]
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短