algorithm - 比较两个不同字典中的值
问题描述
给定两个字典
a={'aabb': ['aabb'], 'abcd': ['abcd', 'abce', 'abcf'], 'abbc': ['abbc']}
b={'aabb': 1, 'abcd': 2, 'abbc': 1}
在示例中从 a 中获取所有值,其中 thr 键在 b 中具有最大值:在 b 'abcd' 中具有最大值,因此我们print(['abcd', 'abce', 'abcf'])
有人可以帮我解决吗?
解决方案
这是问题的python代码,如果有任何问题请告诉我。
a = {'aabb': ['aabb'], 'abcd': ['abcd', 'abce', 'abcf'], 'abbc': ['abbc']}
b = {'aabb': 1, 'abcd': 2, 'abbc': 1}
final_list = []
#fetch the values in dict a
for key, value in a.items():
for i in value:
final_list.append(i)
#fetch maximum value in dict b
max_val = max(b.values())
#fetch the key in dict b that has maximum value and is present in values of
#dict a
for i in final_list:
for key, value in b.items():
if value == max_val:
if key == i:
new_key = i
print(a[new_key])
推荐阅读
- python - 当 git commit-msg 挂钩失败时,如何恢复提交消息?
- unit-testing - Aurelia 单元测试访问组件的 viewModel
- python - 在 Python 中,有没有办法使用 foreach 样式的构造来更新容器?
- ios - 首次启动运行 iOS 12.0.1 的 iPad 时出现 React-native Ios 空白屏幕
- httprequest - C# GetResponse() 方法
- java - 如何在 Java 中使用 SecureRandom 和开关
- python - 多次传递此变量的有效方法
- meteor - 单击编辑按钮时表格行消失
- c++ - 运行时不同的执行策略
- activerecord - 具有可选关系的联合表