字典键和值可分隔numpy数组 [英] Dictionary keys and values to separate numpy arrays
本文介绍了字典键和值可分隔numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字典
Samples = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771}
我想将keys
和values
分成2个numpy
数组.
我尝试了np.array(Samples.keys(),dtype=np.float)
,但出现了错误TypeError: float() argument must be a string or a number
I want to separate the keys
and values
into 2 numpy
arrays.
I tried np.array(Samples.keys(),dtype=np.float)
but i get an error TypeError: float() argument must be a string or a number
推荐答案
您可以使用np.fromiter
从字典键和值视图直接创建numpy
数组:
You can use np.fromiter
to directly create numpy
arrays from the dictionary key and values views:
在python 3中:
In python 3:
keys = np.fromiter(Samples.keys(), dtype=float)
vals = np.fromiter(Samples.values(), dtype=float)
在python 2中:
In python 2:
keys = np.fromiter(Samples.iterkeys(), dtype=float)
vals = np.fromiter(Samples.itervalues(), dtype=float)
这篇关于字典键和值可分隔numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文