gpt4 book ai didi

Python 打印错误 : %d format: a number is required, 不是元组

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:01 25 4
gpt4 key购买 nike

当我尝试打印时出现意外错误:

import numpy as np
a = np.array([11, 21, 31, 41, 51])
it = np.nditer(a, flags=['multi_index'], op_flags=['readwrite'])

while not it.finished:
i = it.multi_index
print("%d %d" % (i, a[i]))
it.iternext()

此代码产生错误:

TypeError: %d format: a number is required, not tuple

但是当我简单地这样做时:

for i in xrange(5):
print("%d %d" % (i, a[i]))

然后我得到了预期的结果:

0 11
1 21
2 31
3 41
4 51

那么为什么我在前面的案例中会收到此错误?

最佳答案

i 不是数字。

In [69]: it.multi_index
Out[69]: (0,)

改用i[0]

关于Python 打印错误 : %d format: a number is required, 不是元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45256090/

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