简体   繁体   中英

Pythonic way to extract specific values from a dictionary based on a subset of keys

I have a set of row dictionaries each has the same keys. I want to create a pointer using a subset of the keys,

mydict = {'g':'1','a':'2','r':'3','c':'24','b':'38'}

The pointer might use the values of 'a','g' and 'c'

pointer = '-'.join([mydict['a'],mydict['g'],mydict['c']])

So the pointer looks like:

2-1-24

Is there a more general approach to accomplish the pulling of values from a dictionary in a particular order

As I am writing this I wonder if it should be on code review as I can clearly accomplish my objective but this code is not very reusable I would like to do something more 'Pythonic'

I did find this question but I don't think it is exactly what I am looking for

for a general approach, you might look to the functional tools, for instance map .

mydict = {'g':'1','a':'2','r':'3','c':'24','b':'38'}
keys = ['a', 'g', 'c']
results = map(mydict.__getitem__, keys)
pointer = '-'.join(results)

(obviously the results part can be inlined into the join, but this felt clearer)

In Python, the 'indexing operator' [] works by calling the collection's __getitem__ method (yes, this does mean that if you define a class with a method called __getitem__ you can use the square brackets to index into it). The map function takes a function argument and a collection, and returns a list equivalent to [f(x) for x in coll]

in fact, the above code is equivalent in function to results = [mydict[x] for x in keys] , but last I looked map was faster.

>>> def t1():  
...  mydict = {'g':'1','a':'2','r':'3','c':'24','b':'38'}  
...  keys = ['a', 'g', 'c']  
...  results = map(mydict.`__getitem__`, keys)  
...  
>>> def t2():  
...  mydict = {'g':'1','a':'2','r':'3','c':'24','b':'38'}  
...  keys = ['a', 'g', 'c']  
...  results = [mydict[x] for x in keys]  
...  
`>>>` timeit.timeit(t1, number = 100000)  
0.061136093994718976  
`>>>` timeit.timeit(t1, number = 100000)  
0.05009100399911404  
`>>>` timeit.timeit(t2, number = 100000)  
0.06633162200159859  
`>>>` timeit.timeit(t2, number = 100000)  
0.06771555900922976  

Hmmm...

pointer = '{data[a]}-{data[g]}-{data[c]}'.format(data=mydict)

Looks a bit better, at least.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM