gpt4 book ai didi

python - 列表列表中子列表的最大和

转载 作者:行者123 更新时间:2023-11-28 21:31:07 25 4
gpt4 key购买 nike

迈出我使用 Python 的第一步。我有一个列表列表,我正在尝试返回所有子列表中总和最大的子列表。现在我只有最大总和本身。例如:此代码返回 18,但我需要返回 [3,3,3,3,3,3]

有什么指示吗?谢谢

def find_biggest(lst):
inner_list_sum = []
for i in range(len(lst)):
inner_list_sum.append(sum(lst[i])) # list of elements sums
return max(inner_list_sum) # I actually need the element itself...not the max sum

print(find_biggest([[1,2,3,4], [1,2,3,3], [1,1], [3,3,3,3,3,3]]))

最佳答案

maxkey=sum 结合使用

例如:

data = [[1,2,3,4], [1,2,3,3], [1,1], [3,3,3,3,3,3]]
print(max(data, key=sum))

输出:

[3, 3, 3, 3, 3, 3]

关于python - 列表列表中子列表的最大和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58862625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com