Multiplying values from two different dictionaries together in Python

前端 未结 3 740
天涯浪人
天涯浪人 2021-01-12 16:43

I have two separate dictionaries with keys and values that I would like to multiply together. The values should be multiplied just by the keys that they have.

i.e.

3条回答
  •  误落风尘
    2021-01-12 17:34

    From my telephone, so bit hard to type code. This should do the trick:

    for key, value in dict1.iteritems():
        if key in dict2:
             dict3[key] = int(dict1[key]) * int(dict2[key])
    

提交回复
热议问题