Python Splitting Dict By Value Of One Of The Keys Indexerror: Index 141 Is Out Of Bounds For Axis 0 With Size 1
This question is an addendum to one that has already been asked: Splitting dict by value of one of the keys I have a dictionary that has 19 keys and each key contains an array of 5
Solution 1:
I used something different, hope you don't mind. I believe it works:
from itertools import compress
data2={key:list(compress(data[key],[i-1for i indata['classifier']])) for key indata.keys()}
data1={key:list(compress(data[key],[i-2for i indata['classifier']])) for key indata.keys()}
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]))
gives:
['yes']
Also, if
data ['classifier'] = [1, 1, 2]
then
[i-1 for i in data['classifier']]
gives:
[0, 0, 1] #evaluates to [False,False,True]
and
[i-2 for i in data['classifier']]
gives:
[-1, -1, 0] #evaluates to [True,True,False]
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 indata['classifier']])) for key indata.keys()} # or just data['classifier']
data1={key:list(compress(data[key],[i + anything for i indata['classifier']])) for key indata.keys()}
Post a Comment for "Python Splitting Dict By Value Of One Of The Keys Indexerror: Index 141 Is Out Of Bounds For Axis 0 With Size 1"