python - 为什么 max(*list) 和 max(list) 在 python 中做同样的事情?

标签 python argument-unpacking

看下面的例子:

>>>f = [[1],[2],[3]]
>>>max(f)
Out[21]: [3]
>>>max(*f)
Out[22]: [3]

解包运算符在这里没有效果,我试图解包一个列表并获得矩阵的最大值(两个暗列表)。

最佳答案

max 的文档提到:

builtin max

max(iterable, *[, key, default])

max(arg1, arg2, *args[, key])

...

If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.

在第一种情况下你有一个位置参数,在第二种情况下你有多个。


为了得到你想要的,这是你可以尝试的矩阵中的最大条目

max(entry for row in matrix for entry in row)

这会将一个参数传递给 max 函数,该函数是一个迭代矩阵中所有条目的生成器 - 触发第一个找到可迭代项最大值的情况。

关于python - 为什么 max(*list) 和 max(list) 在 python 中做同样的事情?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52285862/

相关文章:

python - Pycharm 在配置为使用 virtualenv 解释器时使用基本解释器

python - 解压字典并将其作为关键字参数传递给函数

python - 充当**解包映射的类

python - 参数解包和分配给类变量

Python 脚本在 Linux 上工作正常,在 Windows 上,导致 WindowsError : [Error 5] Access is denied

python - 使用 openCV 检测墙角 - 硬

python - 如何使用 Python 获取数据库中最后插入记录的主键

python - Fabric的run功能在较大的场景下不起作用,在较小的场景下工作正常

python - instance'__dict__ 的关键字参数拆包有哪些陷阱?

python - 函数参数解包错误