Divide the values of two dictionaries in python

后端 未结 4 1291
离开以前
离开以前 2020-12-16 02:34

I have two dictionaries with the same keys and I would like to do division on the values to update or create a new dictionary, keeping the keys intact, with the quotient as

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 03:24

    d1 = { 'a':12 , 'b':10 , 'c':2 }
    d2 = { 'a':0 , 'c':2 , 'b':5}
    d3={x:float(d2[x])/d1[x] for x in d1}
    print d3
    

    output:

    {'a': 0.0, 'c': 1.0, 'b': 0.5}
    

提交回复
热议问题