Python字典理解将一些对复制到一个新的字典 [英] Python Dictionary comprehension to copy some pairs to a new dictionary

查看:790
本文介绍了Python字典理解将一些对复制到一个新的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在播放输入验证时,请特别检查提供的数据是否具有指定的所有必需属性,并丢弃不需要的属性,我做了这样的操作:

 >>> input = {'v1':'val1','a2':'val2','a3':'val3'} 
>>>打印输入
{'v1':'val1','a3':'val3','a2':'val2'}
>>> goodkeys = ['a2','a3']
>>>打印好友
['a2','a3']
>>> output = {}
>>>>对于一个功能:
...输出[a] =输入[a]
...
>>>
>>>打印输出
{'a3':'val3','a2':'val2'}

当然有哪些作品。但是在我看来,这样做可能会更具幽默感。我试过:

 >>> output = {} 
>>>>打印输出
{}
>>> output = {a:v for a in a keys in v in input [a]}
>>>>打印输出
{'a3':'3','a2':'2'}
>>>

我认为工作的半秒钟后,我意识到这些值是错误的。有没有一个漂亮的Python方法?发生了什么事情,Python在哪里得到这些值?

解决方案

你可以使用 词典视图对象

  goodkeys = {'a2','a3'} 
output = {a:input [a] for a in input.viewkeys()& goodkeys}

其中 goodkeys 是一套; dict.viewkeys() 生成一个反映字典内容的集合对象,& 生成这些键和好键之间的交集。这样你只会输入输入字典和一组好的键。



您创建了一个这样的嵌套循环:

  for a goodkeys:
for v in input [a]:
#v是字符串值中的单个字符

演示:

 >>> input = {'v1':'val1','a2':'val2','a3':'val3'} 
>>> goodkeys = {'a2','a3'}
>>> {a:input [a] for a in input.viewkeys()& goodkeys}
{'a3':'val3','a2':'val2'}

由于 input [a] 是一个字符串,你的内部循环会产生每个值的单个字符:

 >>>对于一个功能:
... for v in input [a]:
... print a,v
...
a3 v
a3 a
a3 l
a3 3
a2 v
a2 a
a2 l
a2 2
a2 ,其中一个 v a l 2 与其他人丢弃。字典理解首先设置'a2':'v',然后'a2':'a'等并且只有'a2':'2'保留在输出结尾。



你没有需要使用那个内循环;你可能会坚持:

  {a:输入[v] for a goodkeys} 

除了在中的任何密钥不在 input 你会得到一个 KeyError 。字典视图方法整齐地回避了这个问题。


While playing around with input validation, specifically checking whether the supplied data have all the required attributes specified, and discarding attributes that I don't want, I did something like this:

>>> input = {'v1': 'val1', 'a2':'val2', 'a3':'val3'} 
>>> print input
{'v1': 'val1', 'a3': 'val3', 'a2': 'val2'}
>>> goodkeys = ['a2', 'a3']
>>> print goodkeys
['a2', 'a3']
>>> output = {}
>>> for a in goodkeys:    
...   output[a] = input[a]
... 
>>> 
>>> print output
{'a3': 'val3', 'a2': 'val2'}

Which of course works. But it occurred to me that it may be possible to do this in a more pythonic way. I tried:

>>> output = {}
>>> print output
{}
>>> output = { a:v for a in goodkeys for v in input[a] }
>>> print output
{'a3': '3', 'a2': '2'}
>>> 

Which for half a second I thought worked, then I realized that the values are wrong. Is there a pretty Python method? And what happened, where did Python get those values?

解决方案

You could use dictionary view objects:

goodkeys = {'a2', 'a3'}
output = {a: input[a] for a in input.viewkeys() & goodkeys}

where goodkeys is a set; dict.viewkeys() produces a set-like object that reflects the contents of the dictionary, and & goodkeys produces the intersection between those keys and the goodkeys set. That way you only ever produce keys that are both in the input dictionary and the set of good keys.

You created a nested loop like this:

for a in goodkeys:
    for v in input[a]:
        # v is a single character in the string value

Demo:

>>> input = {'v1': 'val1', 'a2':'val2', 'a3':'val3'} 
>>> goodkeys = {'a2', 'a3'}
>>> {a: input[a] for a in input.viewkeys() & goodkeys}
{'a3': 'val3', 'a2': 'val2'}

Since input[a] is a string, your inner loop produced the individual characters of each value:

>>> for a in goodkeys:
...     for v in input[a]:
...         print a, v
... 
a3 v
a3 a
a3 l
a3 3
a2 v
a2 a
a2 l
a2 2

Keys must be unique, so for a2, one of v, a, l and 2 is picked with the others discarded. The dictionary comprehension sets 'a2': 'v' first, then 'a2': 'a', etc. and only 'a2': '2' remains in the output, in the end.

You didn't need to use that inner loop; you could have stuck with:

{a: input[v] for a in goodkeys}

except that if there are any keys in goodkeys that are not in input you'd get a KeyError. The dictionary view approach neatly sidesteps that issue.

这篇关于Python字典理解将一些对复制到一个新的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆