numpy数组中的字典? [英] Dictionary in a numpy array?

查看:388
本文介绍了numpy数组中的字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问数组中的字典?

How do I access the dictionary inside the array?

import numpy as np
x = np.array({'x': 2, 'y': 5})

我最初的想法:

x['y']




索引错误:无效索引

Index Error: not a valid index



x[0]




索引错误:数组索引太多

Index Error: too many indices for array


推荐答案

你有一个对象dtype的0维数组。制作这个数组可能是一个错误,但是如果你想要使用它,你可以通过使用无索引的元组索引数组来提取字典:

You have a 0-dimensional array of object dtype. Making this array at all is probably a mistake, but if you want to use it anyway, you can extract the dictionary by indexing the array with a tuple of no indices:

x[()]

或通过调用数组的 item 方法:

or by calling the array's item method:

x.item()

这篇关于numpy数组中的字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆