Python 用于分隔 numpy 数组的字典键和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23668509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:18:06  来源:igfitidea点击:

Dictionary keys and values to separate numpy arrays

pythonarraysnumpydictionary

提问by VeilEclipse

I have a dictionary as

我有一本字典

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}

I want to separate the keysand valuesinto 2 numpyarrays. I tried np.array(Samples.keys(),dtype=np.float)but i get an error TypeError: float() argument must be a string or a number

我想将keysvalues分成2个numpy数组。我试过了,np.array(Samples.keys(),dtype=np.float)但出现错误TypeError: float() argument must be a string or a number

采纳答案by ankostis

You can use np.fromiterto directly create numpyarrays from the dictionary key and values views:

您可以使用从字典键和值视图np.fromiter直接创建numpy数组:

In python 3:

在python 3中:

keys = np.fromiter(Samples.keys(), dtype=float)
vals = np.fromiter(Samples.values(), dtype=float)

In python 2:

在蟒蛇 2 中:

keys = np.fromiter(Samples.iterkeys(), dtype=float)
vals = np.fromiter(Samples.itervalues(), dtype=float)

回答by 1478963

keys = np.array(dictionary.keys())
values = np.array(dictionary.values())

回答by A.J. Uppal

Just assign all of the values to a list, and then convert to a np.array().

只需将所有值分配给一个列表,然后转换为np.array().

import numpy as np

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 = np.array(Samples.keys())
vals = np.array(Samples.values())

Or, if you want to iterate over it:

或者,如果你想迭代它

import numpy as np

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 = vals = []

for k, v in Samples.items():
    keys.append(k)
    vals.append(v)

keys = np.array(keys)
vals = np.array(vals)

回答by pratyaksh

On python 3.4, the following simply works:

在 python 3.4 上,以下简单有效:

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 = np.array(list(Samples.keys()))
values = np.array(list(Samples.values()))

The reason np.array(Samples.values())doesn't give what you expect in Python 3 is that in Python 3, the values() method of a dict returns an iterable view, whereas in Python 2, it returns an actual list of the keys.

原因np.array(Samples.values())没有给出您在 Python 3 中所期望的内容,因为在 Python 3 中,dict 的 values() 方法返回一个可迭代视图,而在 Python 2 中,它返回一个实际的键列表。

keys = np.array(list(Samples.keys()))will actually work in Python 2.7 as well, and will make your code more version agnostic. But the extra call to list()will slow it down marginally.

keys = np.array(list(Samples.keys()))实际上也可以在 Python 2.7 中工作,并使您的代码与版本无关。但是额外的调用list()会稍微减慢它的速度。

回答by Hosana Gomes

In Python 3.7:

在 Python 3.7 中:

import numpy as np

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 = np.array(list(Samples.keys()))
vals = np.array(list(Samples.values()))

Note: It's important to say that in this Python version dict.keys()and dict.values()return objects of type dict_keysand dict_values, respectively.

注:说在这个Python版本是很重要的dict.keys(),并dict.values()返回对象类型dict_keysdict_values分别。