1

I want to iterate over a numpy array and do some calculations on the values. However, things are not as expected. To show what I mean, I simply wrote this code to read values from a numpy array and move them to another list.

a = array([1,2,1]).reshape(-1, 1)
u = []
for i in np.nditer(a):
    print(i)
    u.append(i)
print(u)

According to tutorial, nditer points to elements and as print(i) shows, i is the value. However, when I append that i to an array, the array doesn't store the value. The expected output is u = [1, 2, 1] but the output of the code is

1
2
1
[array(1), array(2), array(1)]

What does array(1) mean exactly and how can I fix that?

P.S: I know that with .tolist() I can convert a numpy array to a standard array. However, in that code, I want to iterate over numpy elements.

7
  • If you would print the type of i, you would see that its of type <class 'numpy.ndarray'> Commented Jan 24, 2022 at 14:24
  • Is there a reason why you reshaped? From that you get an object with shape (3, 1) and if you iterate over those items you get an array with one element in it. Commented Jan 24, 2022 at 14:27
  • the real question is why would you use np.nditer on the first place? Commented Jan 24, 2022 at 14:30
  • By default, iterating over an array of dimension d>1 only iterates through the first axis, and considers it as an array of dimension d-1. You're reshaping a to be an array of shape (3,1), which makes it a d=2 array, so each iteration will return a d=1 array. If you flatten it first, it will do the trick : for i in a.flatten(): u.append(i) Commented Jan 24, 2022 at 14:32
  • @mozway: I tried nditer because I saw that in some tutorials. In fact I didn't check the type of i and that print(i) misguided me. Commented Jan 24, 2022 at 14:38

3 Answers 3

1

As already explained in your previous question, numpy.nditer yields numpy arrays. What is shown by print is only the representation of the object, not the content or type of the object (e.g., 1 and '1' have the same representation, not the same type).

import numpy as np
a = np.array([1,2,1]).reshape(-1, 1)

type(next(np.nditer(a)))
# numpy.ndarray

You just have a zero-dimensional array:

np.array(1).shape
# ()

There is no need to use numpy.nditer here. If you really want to iterate over the rows of your array with single column (and not use tolist), use:

u = []
for i in a[:,0]:
    u.append(i)
u
# [1, 2, 1]
Sign up to request clarification or add additional context in comments.

1 Comment

I would also add that calling string on a python list calls the __repr__ method on each element inside. For some items, __str__ and __repr__ are the same, but this is not the case
1

numpy.nditer actually returns a numpy array. If you want the actual value of this item, you can use the built in item() function:

a = array([1,2,1]).reshape(-1, 1)
u = []
for i in np.nditer(a):
    u.append(i.item())
print(u)

Comments

0

A pure python equivalent of what's happening with your append is:

In [75]: alist = []
    ...: x = [0]
    ...: for i in range(3):
    ...:     x[0] = i
    ...:     print(x)
    ...:     alist.append(x)
[0]
[1]
[2]
In [76]: alist
Out[76]: [[2], [2], [2]]
In [77]: x
Out[77]: [2]

x is modified in each loop, but only a reference is saved. The result is that all elements of the list are the same object, and display its last value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.