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

标签 python python-3.x python-requests

迈出我使用 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/

相关文章:

python - Python 的 Tkinter 可以在按钮小部件中同时显示文本和图像吗?

python - HEAD 和 GET http 请求为同一 URL 返回不同的状态代码

python - SSLError : bad handshake, Python 请求

python - 将 FastAPI 中的请求记录到项目目录中的特定文件中

python - Celeryd 启动太多进程

python - Python中对数计算的底数会影响速度吗?

python - Perl中相当于$|=1的python是什么

python-3.x - Mypy + flake8 : Is there any way to surpress warning of `F821 undefined name`

python - 如何解释这两个 "="以及整个表达式?

python - 将 numpy 数组图像转换为与 requests.get 相同的格式