【发布时间】:2018-05-04 19:15:54
【问题描述】:
尝试在 Python3 Jupyter notebook 中运行此代码:
t = namedtuple('a', 'b')
a = [1,0,1]
b = [1,1,1]
Out, In = np.asanyarray(a), np.asanyarray(b)
t(Out.shape[0], *In.shape)
返回错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-151-7955ff03a60d> in <module>()
3 b = [1,1,1]
4 Out, In = np.asanyarray(a), np.asanyarray(b)
----> 5 t(Out.shape[0], *In.shape)
TypeError: __new__() takes 2 positional arguments but 3 were given
是否可以使用两个参数创建命名元组?
更新:
为什么这不会导致类似的问题:
t = namedtuple('ps', 'Out In S')
a = np.asanyarray([[1]])
b = np.asanyarray([[1]])
d = t(a.shape[0], *b.shape)
d
计算:
ps(Out=1, In=1, S=1)
更新 2:
我想我现在明白了 namedtuple('ps', 'Out In S') 转换为 namedtuple('name_of_tuple', 'tuple_values_seperated_by_spaces')
【问题讨论】:
-
命名元组的第一个参数,是命名元组的名称,不是参数。
-
您错误地使用了
namedtuple。 Check the docs on what the parameters should be.
标签: python python-3.x namedtuple