简体   繁体   English

基于键子集的从字典中提取特定值的Python方法

[英]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' 指针可能使用'a','g'和'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' 当我写这篇文章时,我想知道是否应该对代码进行审查,因为我可以很清楚地实现我的目标,但是这段代码不是很可重用,我想做一些更“ 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 . 对于一般方法,您可能需要使用功能性工具,例如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). 在Python中,“索引运算符” []通过调用集合的__getitem__方法来工作(是的,这的确意味着如果您使用名为__getitem__的方法定义类,则可以使用方括号对其进行索引)。 The map function takes a function argument and a collection, and returns a list equivalent to [f(x) for x in coll] map函数接受一个函数参数和一个集合,并返回一个等效于[f(x)for coll中的x的列表]

in fact, the above code is equivalent in function to results = [mydict[x] for x in keys] , but last I looked map was faster. 实际上,上面的代码在功能上等效于results = [mydict[x] for x in keys] ,但是最后我看到map更快。

>>> 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. 看起来至少好一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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