繁体   English   中英

有没有办法用匹配的键将两个字典的对应值相互除法?

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

我有以下两个字典:

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

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

有没有办法将dict1的元素除以dict1的相应元素, dict2以字典形式得到以下结果?

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

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

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

暂无
暂无

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

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