简体   繁体   中英

Is there a way to divide with each other the corresponding values of two dictionaries with matching keys?

I have the following two dictionaries:

dict1={'Hewey': 4, 'Dewey': 24, 'Louie': 15, 'Donald': 56, 'Scrooge': 9}

dict2={'Hewey': 2, 'Dewey': 6, 'Louie': 3, 'Donald': 2, 'Scrooge': 3}

Is there a way to divide the elements of dict1 by the corresponding elements of dict2 , such that I get the following result in dictionary form?

dict3={'Hewey': 2, 'Dewey': 4, 'Louie': 5, 'Donald': 28, 'Scrooge': 3}

如果您的密钥总是重叠,那么这就足够了:

dict3 = {k: (dict1[k] / dict2[k]) for k in dict1}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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