简体   繁体   English

比较两个字典并乘以它们的值

[英]Comparing two dictionaries and multiplying their values

I can't seem to be able to fix this problem in python, and I would be really happy if someone could help me. 我似乎无法用python解决此问题,如果有人可以帮助我,我将非常高兴。 I basically have two dictionaries. 我基本上有两个字典。

dict_a = { "poop" : 2, "eggs": 3, "pee": 3, "debt": 4 }
dict_b = { "poop" : 10, "pee": 9}

for word, number in dict_a.items():
     if word in dict_b.keys():
           points = dict_a[word] * dict_b[key]
prints(points)

My problem is that it doesnt seem to work hehe. 我的问题是,它似乎无法正常工作。 When I'm comparing my two dictionaries, even if they have the same keys, they do not match. 当我比较两个字典时,即使它们具有相同的键,它们也不匹配。 Is there any easier way in seeing if two dictionaries have the same keys, and then multiply the keys' values with each other? 有没有更简单的方法来查看两个字典是否具有相同的键,然后将键的值彼此相乘?

I want the result to be points = 47 Because poop has its values 2*10 and pee has 3*9. 我希望结果为点= 47,因为便便的值为2 * 10,而小便的值为3 * 9。

Thank you very much in advance :) 提前非常感谢您:)

try this. 尝试这个。

def mult_dicts(dict1, dict2):
  result_dict = {}
  for word in dict1:
    result_dict[word] = dict1[word]
    if word in dict_b:
      result_dict[word] *= dict2[key]
    else:
      result_dict[word] = dict2[key]
  return result_dict

Or, if you only want it to contain entries when the key is in both dictionaries: 或者,如果您只希望当键在两个字典中时都包含条目:

def mult_dicts(dict1, dict2):
  result_dict = {}
  for word in dict1:
    if word in dict_b:
      result_dict[word] = dict1[word] * dict2[key]
  return result_dict

here you go: 干得好:

>>> for x in set(dict_a.keys() + dict_b.keys()):
...     print(x,dict_b.get(x,1)*dict_b.get(x,1))
... 
('pee', 27)
('poop', 20)
('debt', 4)
('eggs', 3)

if you want a dictionary: 如果您想要一本字典:

>>> { x:dict_a.get(x,1)*dict_b.get(x,1) for x in set(dict_a.keys() + dict_b.keys())}
{'pee': 27, 'poop': 20, 'debt': 4, 'eggs': 3}

if you want list: 如果要列出:

>>> [ [x,dict_a.get(x,1)*dict_b.get(x,1)] for x in set(dict_a.keys() + dict_b.keys())]
[['pee', 27], ['poop', 20], ['debt', 4], ['eggs', 3]]

total points: 总积分:

>>> sum([ dict_a.get(x,1)*dict_b.get(x,1) for x in set(dict_a.keys()) & set(dict_b.keys())])
47

remove sum to see values: 删除总和以查看值:

>>> [ dict_a.get(x,1)*dict_b.get(x,1) for x in set(dict_a.keys()) & set(dict_b.keys())]
[27, 20]

This is assuming, of course, that non present values have a cardinality of 1. 当然,这是假定非现值的基数为1。

dict_a = { "poop" : 2, "eggs": 3, "pee": 3, "debt": 4 }
dict_b = { "poop" : 10, "pee": 9}
result = {}

for key in set(dict_a).union(dict_b):
    result[key] = dict_a.get(key, 1) * dict_b.get(key, 1)

You may also use a defaultdict to prevent the explicit get(x, 1) but its mainly a matter of preference at this point. 您也可以使用defaultdict来阻止显式的get(x, 1)但此时主要是优先考虑的问题。

       dict_a = { "poop" : 2, "eggs": 3, "pee": 3, "debt": 4 }
       dict_b = { "poop" : 10, "pee": 9}

x=raw_input("Word: ")
if x in dict_a:
    point=dict_a[x]*dict_b[x]
print point

Result: word: poop 结果:字:大便

20 20

Edit then: 然后编辑:

    dict_a = { "poop" : 2, "eggs": 3, "pee": 3, "debt": 4 }
    dict_b = { "poop" : 10, "pee": 9}


for x in dict_a:
    for y in dict_b:
        point=dict_a[x]*dict_b[y]
        print point

Result: 27 30 18 20 36 40 27 30 结果: 27 30 18 20 36 40 27 30

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

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