Python按键其中之一的值分割dict的错误:索引141对于大小为1的轴0超出范围 [英] Python splitting dict by value of one of the keys IndexError: index 141 is out of bounds for axis 0 with size 1

查看:82
本文介绍了Python按键其中之一的值分割dict的错误:索引141对于大小为1的轴0超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是已经提出的问题的附录:

This question is an addendum to one that has already been asked: Splitting dict by value of one of the keys

我有一个包含19个键的字典,每个键包含51000个值或观察值的数组.键之一是分组/分类键,其值可以是1或2.我想做的就是将字典拆分成两个新的字典.一个用于分类键为1时,另一个用于分类键为2时.

I have a dictionary that has 19 keys and each key contains an array of 51000 values or observations. One of the keys is a grouping/classification key and its value can be either 1 or 2. What I would like to do is split the dictionary into two new dictionaries. One for when the classification key is 1 and another one for when the classification is 2.

data = {'variable 1': array([ 90, 91, 89, ...
           .
           .
           .
       'variable 18': array([0.1, 0.02, 0.4, ...
       'classifier': array([1, 1, 2, ...
       }

我已尝试针对上述问题执行georgesl发布的解决方案:

I have tried doing the solution posted by georgesl for the question mentioned above:

data1 = [ { key : data[key][idx] for key in data.keys() }  for idx, x in enumerate(data["id"]) if x == 1 ]

但是,当我运行它时,出现以下错误:

However, when I run this I get the following error:

 IndexError: index 141 is out of bounds for axis 0 with size 1

我还尝试使用以下方法将数组转换为列表:

I also tried to convert the arrays to a list using:

data2 = {}
for key in data.keys():
     data[key] = data[key].tolist()

但是当我通过发布的解决方案运行它时,会产生以下错误:

But this yields the following error when I run it through the posted solution:

IndexError: list index out of range

我可能错过了一些确实很明显的东西,但是我一生都无法弄清楚是什么.我愿意接受任何建议.

I am probably missing something really obvious but can't for the life of me figure out what. I am open for any suggestions.

推荐答案

我使用了其他方法,希望您不要介意.我相信它会起作用:

I used something different, hope you don't mind. I believe it works:

from itertools import compress
data2={key:list(compress(data[key],[i-1 for i in data['classifier']])) for key in data.keys()}
data1={key:list(compress(data[key],[i-2 for i in data['classifier']])) for key in data.keys()}

这是我第一次使用 itertools.compress 我不是专家.无论如何,它就像面具一样工作:

It is my first time using itertools.compress so I am not an expert. Anyway, it works like a mask so something like:

>>> list(compress(['no','yes'],[False, True]))

给予:

['yes']

如果

data ['classifier'] = [1, 1, 2]

然后

[i-1 for i in data['classifier']]

给予:

[0, 0, 1] #evaluates to [False,False,True]

[i-2 for i in data['classifier']]

给予:

[-1, -1, 0] #evaluates to [True,True,False]

现在,假设您要在分类器中使用0和1,并且如果分类键为0,则有data1,这是您的代码:

Now, assuming you wanted 0 and 1 in classifier and if the classification key is 0 you have data1, this is your code:

data2={key:list(compress(data[key],[i for i in data['classifier']])) for key in data.keys()} # or just data['classifier']
data1={key:list(compress(data[key],[i + anything for i in data['classifier']])) for key in data.keys()}

这篇关于Python按键其中之一的值分割dict的错误:索引141对于大小为1的轴0超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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