Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing values in python dictionary

I have a huge dictionary with over a 1000 keys and each value is over 600 000 int long. Now, I need to extract some of these integers, so from 600 000 I want to go to let's say 5k. But it can't be random 5k, they have to be at very specific positions. Due to the fact that 5k is still a little too big to extract it by hand, I need to use a list of indices that will indicate which integers in the value should be taken out. I have tested extraction on small lists, with indices [1,3,5,7,9] and long_val ['a','b','c','d','e','f','g','h','i','j','k'] then I can do that:

for each in xrange(len(long_val)):
    print indices[long_val[each]]

and I get b,d,f,h and j (as required).

Now, it's not as simple when it comes to dealing with dictionaries (where long_val) is replaced by actual dictionary value). I have tried that:

for keys,values in dict_gtps.iteritems():
    for each in xrange(len(values)):
        abs_new[keys]=pos_3[values[each]]

But I'm getting "Index out of range" error message.

like image 719
branwen85 Avatar asked May 18 '26 04:05

branwen85


2 Answers

If you are using the same indices, it will be more efficient to use itemgetter(*indices)

>>> from operator import itemgetter
>>> indices =  [1,3,5,7,9]
>>> long_val = ['a','b','c','d','e','f','g','h','i','j','k'] 
>>> ig = itemgetter(*indices)
>>> ig(long_val)
('b', 'd', 'f', 'h', 'j')

so

from operator import itemgetter
ig = itemgetter(*indices)
for k, v in dict_gtps.iteritems():
    print ig(v)
    abs_new[k] = ig(v)

you could also use a dict comprehension

abs_new = {k:ig(v) for k,v in dict_gtps.iteritems()}
like image 127
John La Rooy Avatar answered May 20 '26 19:05

John La Rooy


Assuming I read your requirements correctly, you could try:

for key, value in dict_gtps.iteritems():
  abs_new[key] = [value[i] for i in indices]
like image 45
Shawn Chin Avatar answered May 20 '26 18:05

Shawn Chin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!