【发布时间】:2017-12-28 14:14:27
【问题描述】:
我在尝试打印时遇到意外错误:
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
那么为什么在前面的情况下会出现这个错误?
【问题讨论】:
-
试试
print(repr((i, a[i]))) -
这里不需要
nditer-ndindex可以
标签: python numpy ipython typeerror