我正在使用此代码,直接来自 scikit-learn 页面。它创建了一个字典:
symbol_dict = {
'TOT': 'Total',
'XOM': 'Exxon',
'CVX': 'Chevron',}
symbols, names = np.array(symbol_dict.items()).T
但我收到一个错误:
TypeError: iteration over a 0-d array
这段代码直接来自示例代码,所以我不知道出了什么问题。
最佳答案
正如 user2357112 所说,在 Python 3 中 dict.items()
返回字典 View 对象而不是键值对列表( the difference is explained here )。包裹在 list()
创建一个列表,NumPy 可以将其转换为数组:
np.array(list(symbol_dict.items()))
关于python-3.x - TypeError : iteration over a 0-d array, 使用 numpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21198602/