gpt4 book ai didi

python - namedtuple 需要 3 个参数?

转载 作者:行者123 更新时间:2023-12-05 08:55:01 28 4
gpt4 key购买 nike

<分区>

尝试在 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')

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com