简体   繁体   English

如何在两个词典中映射相同的项目并生成其相应键的值列表

[英]How to map identical items in two dictionaries and produce a list of values of their corresponding keys

My goal is to map the small dictionary to the large one and return a list of values in the large dictionary that bears corresponding keys to the small dictionary. 我的目标是将小字典映射到大字典并返回大字典中的值列表,该字典带有小字典的相应键。

x={'red':0.25, 'yellow':0.05, 'pink':0.35, 'brown':0.22, 'blue':0.13}
y={'red':2, 'blue':3, 'yellow':1}

My code keeps giving me a list of the full values of large dictionary. 我的代码不断给我一个大字典的完整值列表。

for b in x.keys():
    if b in y.keys():
        k=y.values()
print k

output: [0.35, 0.22, 0.13, 0.05, 0.25]

Desired output:
[0.25,0.13,0.05]

Any suggestions? 有什么建议么? Thanks. 谢谢。

Something like this? 像这样的东西? I assume that the order does not matter since these are dictionaries. 我认为订单无关紧要,因为这些是字典。 Also, assuming that since you seem to be iterating over x.keys() , then, there may be some values in y that are not present in x and you don't want them mapped. 此外,假设您似乎在迭代x.keys() ,那么, y中可能存在一些x中不存在的值,并且您不希望它们映射。

>>> x={'red':0.25, 'yellow':0.05, 'pink':0.35, 'brown':0.22, 'blue':0.13}
>>> y={'red':2, 'blue':3, 'yellow':1}
>>> [val for elem, val in x.items() if elem in y]
[0.13, 0.05, 0.25]

If there are no values in y that are not in x , then you could simply iterate over the y dictionary. 如果y中没有不在x ,那么您可以简单地遍历y字典。

>>> [x[key] for key in y]
[0.13, 0.05, 0.25]

PS - The problem in your code is that everytime you find a match, you assign the whole list of y.values() to k , hence ending up with a complete list of values. PS - 代码中的问题是,每次找到匹配项时,都会将整个y.values()列表分配给k ,从而最终得到一个完整的值列表。 You could modify your code to look something like this 你可以修改你的代码看起来像这样

>>> k = []
>>> for b in x.keys():
    if b in y.keys():
        k.append(x[b])


>>> k
[0.13, 0.05, 0.25]

Although, iterating over the dictionary gives a similar result, like follows 虽然,迭代字典会给出类似的结果,如下所示

>>> for b in x:
    if b in y:
        k.append(x[b])


>>> k
[0.13, 0.05, 0.25]
k = [x[key] for key in y.keys()]

The issue with your code is, you are assigning y.values() to k . 您的代码的问题是,您将y.values()分配给k So your k is nothing but the list of values of dict y . 所以你的k只不过是dict y的值列表。 You can modify your code like this to make it work: 您可以像这样修改代码以使其工作:

k = []
for b in x.keys():
    if b in y.keys():
        k.append(x[b])
print k

You can also use List comprehension: 您还可以使用列表理解:

>>> x={'red':0.25, 'yellow':0.05, 'pink':0.35, 'brown':0.22, 'blue':0.13}
>>> y={'red':2, 'blue':3, 'yellow':1}

>>> [x[key] for key in y]
[0.13, 0.05, 0.25]

I would think about this as the intersection of two sets, then pass the subset to map all of the corresponding values. 我会将此视为两组的交集,然后传递子集以映射所有相应的值。 This way it would be much easier to understand than list comprehension. 这样,它比列表理解更容易理解。

>>> map(x.get, set(x) & set(y))
>>> [0.13, 0.05, 0.25]

Of course you can do the same with list comprehension: 当然,你可以对列表理解做同样的事情:

>>> [x.get(i) for i in set(x) & set(y)]
>>> [0.13, 0.05, 0.25]

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

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