python - python 中的 max - 错误缺少 1 个必需的位置参数 : 'b'

标签 python arguments max typeerror

<分区>

我正在尝试在 python 上做一个练习(我的版本是 python33 练习内容如下:

Write a function find_longest_word1() that takes a list of words and returns the length of the longest one. Use only higher order functions.

我的尝试是:

def find_longest_word1(a):
    out = max(list(map(len,a)))
    print (out)
find_longest_word1(["This","is","a","listing"])

我得到的错误是:

max() missing 1 required positional argument 'b'

我已经传递了一个列表作为参数。我是否需要为 max 函数提供一些其他参数?

最佳答案

您正在调用的 max() 函数不是 standard built-in ,但是是其他一些名为 max() 的函数:内置函数没有名为 b 的参数,因此错误消息不可能是关于它的。

请仔细检查您是否已定义(或导入)名为 max() 的函数。如果我猜的话,我希望定义看起来像这样:

def max(a, b):
   ...

您的代码在这里工作正常(使用内置的 max()):

In [50]: a = ["This", "is", "a", "listing"]

In [51]: max(list(map(len, a)))
Out[51]: 7

顺便说一句,调用 list() 是不必要的:

In [52]: max(map(len, a))
Out[52]: 7

关于python - python 中的 max - 错误缺少 1 个必需的位置参数 : 'b' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27669052/

相关文章:

python - 在 Python 中播放期间更改音频的音高(和速度)

python - 如何舍入python中的两位有效数字?

ios - 在@selector 方法中传递参数

r - 为什么 range() 函数比 min 和 max 的组合慢?

php - MySQL 中基于另一个字段的最大值更新字段的嵌套 select 语句给出错误

python - 生成巨大列表的随机排列(在 Python 中)

带有 Gstreamer 管道的 Python

javascript - 语法错误 : missing ) after argument list in cart. js

WordPress,wp_query 中的条件参数,OR

filtering - 在杂乱FFT中查找相关峰