# 修復 Python 中 NumPy 的 0-D 陣列上的迭代錯誤

Vaibhav Vaibhav 2022年5月18日

## 如何修復 Python NumPy 中的 TypeError：迭代 0-d 陣列錯誤

import numpy as np

data = {
"AB": 1.01,
"CD": 2.02,
"EF": 3.03,
"GH": 4.04,
"IJ": 5.05,
}

keys, values = np.array(data.items()).T
print(keys)
print(values)

Traceback (most recent call last):
File "<string>", line 11, in <module>
TypeError: iteration over a 0-d array

import numpy as np

data = {
"AB": 1.01,
"CD": 2.02,
"EF": 3.03,
"GH": 4.04,
"IJ": 5.05,
}
print(type(list(data.items())))
keys, values = np.array(list(data.items())).T
print(keys)
print(values)

<class 'list'>
['AB' 'CD' 'EF' 'GH' 'IJ']
['1.01' '2.02' '3.03' '4.04' '5.05']

import numpy as np

data = {
"AB": 1.01,
"CD": 2.02,
"EF": 3.03,
"GH": 4.04,
"IJ": 5.05,
}
print(type(tuple(data.items())))
keys, values = np.array(tuple(data.items())).T
print(keys)
print(values)

<class 'tuple'>
['AB' 'CD' 'EF' 'GH' 'IJ']
['1.01' '2.02' '3.03' '4.04' '5.05']

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.