python - 创建命名元组列表时出现类型错误 : __new__() takes exactly 2 arguments (3 given)

标签 python namedtuple

我想要完成的是创建包含命名元组的列表,所有这些都在循环中。我的代码:

from collections import namedtuple

def selectMatch(self):
    match = namedtuple('ssid', 'quality')
    matches = []
    for point in self.discoverMatch():
            print(point)
            if point.ssid.startswith(''):
                    matches.append(match(point.ssid, point.quality))
    print([x.ssid for x in matches])
    return matches

结果,我在标题中提到了 TypeError。我的目标是将命名元组保存到列表中,但它说我给出了很多论点,现在我有点困惑。

最佳答案

namedtuple接受名称和字段名称列表:

collections.namedtuple(typename, field_names, *, verbose=False, rename=False, module=None)

所以你想要match = nametuple('match', ['ssid', 'quality'])

关于python - 创建命名元组列表时出现类型错误 : __new__() takes exactly 2 arguments (3 given),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45221751/

相关文章:

python - namedtuple 的相等重载

Python 无法识别 pocketsphinx 的解码器

python - 使用正则表达式从字符串中删除部分

python - 在 dask.read_sql_table 中添加 application_name 作为参数时出错

python - 在创建实例之前修改 typing.NamedTuple 的参数

python - 从命名元组列表中写入 numpy.array()

python - Django 中 isinstance() arg 2 必须是类型或类型元组错误

python - 在 Keras 中显示 'y' 的预测值和相应的实际 'x' 值

python - 如何使用 Python namedtuples 将行插入 mySQL 数据库

python - 如何检查对象是否是命名元组的实例?