TypeError: ‘numpy.int64′ object is not iterable ,’int’ object is not iterable

If you want to use a list to dynamically add numpy type data, as shown in the following code, you will find that the error type error is: ‘ numpy.int64 ‘ object is not iterable 

a = []
b = np.array([1,2,3])
a.extend(b[0])
a.extend(b[1])
a.extend(b[2])
print(a)

The numpy data is converted to the list type, as follows:

a = []
b = np.array([1,2,3])
a.extend(b[0].tolist())
a.extend(b[1].tolist())
a.extend(b[2].tolist())
print(a)

Error found: typeerror: ‘Int’ object is not Iterable

By printing the type of ‘B [0]. Tolist ()’, we find that the type of ‘B [0]. Tolist ()’ is’ Int ‘, that is, it does not convert’ B [0]. Tolist () ‘to list type

Then modify the code as follows, change ‘B [0]. Tolist ()’ to list type by adding a bracket []

a = []
b = np.array([1,2,3])
a.extend([b[0].tolist()])
a.extend([b[1].tolist()])
a.extend([b[2].tolist()])
print(a) #[1, 2, 3]

————————————————————————-Dividing line————————————————————————————————–

Later, I found that the following code can also be used directly:

a = []
b = np.array([1,2,3])
a.extend([b[0]])
a.extend([b[1]])
a.extend([b[2]])
print(a) #[1, 2, 3]

This is because the ‘B [0]’ is changed from numpy data type to list type by adding a bracket []

Read More: