3

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.

4

3 回答 3

5

如果您使用相同的索引,使用起来会更有效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')

所以

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

你也可以使用字典理解

abs_new = {k:ig(v) for k,v in dict_gtps.iteritems()}
于 2012-10-10T14:39:08.297 回答
3

假设我正确阅读了您的要求,您可以尝试:

for key, value in dict_gtps.iteritems():
  abs_new[key] = [value[i] for i in indices]
于 2012-10-10T14:39:52.587 回答
1

您的示例代码有缺陷,索引和 long_val 的值颠倒了。

 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]]

抛出一个TypeError. 它应该是:

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]] 

基于此,很明显为什么您的字典函数会引发范围错误,因为您输入了错误的变量。我会让你自己尝试修复代码。

/edit 供后代使用 此外,由于索引中的值是整数,因此您实际上不需要使用 xrange--

for i in indices:
    print long_val[i]

简单得多。

于 2012-10-10T14:45:14.657 回答