小编way*_*tir的帖子

Python打印错误:%d格式:需要数字,而不是元组

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

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()
Run Code Online (Sandbox Code Playgroud)

此代码生成错误:

TypeError: %d format: a number is required, not tuple
Run Code Online (Sandbox Code Playgroud)

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

for i in xrange(5):
    print("%d %d" % (i, a[i]))
Run Code Online (Sandbox Code Playgroud)

然后我得到预期的结果:

0 11
1 21
2 31
3 41
4 51
Run Code Online (Sandbox Code Playgroud)

那么为什么在之前的情况下会出现此错误呢?

python numpy ipython typeerror

4
推荐指数
1
解决办法
5056
查看次数

标签 统计

ipython ×1

numpy ×1

python ×1

typeerror ×1