Python print error: %d format: a number is required, not tuple

Viewed 4415

I get an unexpected error when I try to print:

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()

This code generates the error:

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

But when I simply do this:

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

Then I get the expected result:

0 11
1 21
2 31
3 41
4 51

So why do I get this error in the earlier case?

4 Answers
Related