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.
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()}
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]
Your example code is flawed, indices and long_val have their values reversed.
indices = [1,3,5,7,9]
long_val = ['a','b','c','d','e','f','g','h','i','j','k']
for each in xrange(len(long_val)):
print indices[long_val[each]]
throws a TypeError
. It should be:
indices = [1,3,5,7,9]
long_val = ['a','b','c','d','e','f','g','h','i','j','k']
for each in xrange(len(indices)):
print long_val[indices[each]]
Based on that, it should be pretty obvious why your dictionary function throws a range error, you're feeding it the wrong variable. I'll leave you to try and fix the code yourself.
/edit for posterity Also, since the values in indices are integers, you don't actually need to use xrange--
for i in indices:
print long_val[i]
Much simpler.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.