python对于字典_Python字典理解将一些对复制到一个新的字典

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?

解决方案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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值