我想知道是否有人可以帮助我解决我遇到的 Python 问题。我有四个列表,每个列表都包含 float (小数)。我正在添加每个列表包含的所有 float 。我坚持的部分是我想知道四个列表中哪一个的总和更高。我知道我可以使用 if 语句,但有没有人知道更有效的方法。例如:
foodmart = [12.33,5.55]
nike = [42.20,69.99]
gas_station = [0.89,45.22]
toy_store = [10.99,15.32]
最佳答案
使用max()
:
>>> max(foodmart,nike,gas_station,toy_store, key=sum)
>>> [42.2, 69.99]
help()
在 max
上:
max(iterable[, key=func]) -> value
max(a, b, c, ...[, key=func]) -> value
With a single iterable argument, return its largest item. With two or more arguments, return the largest argument.
关于python - 如何获得元素总和较高的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14262112/