首页 > 解决方案 > 比较两个不同字典中的值

问题描述

给定两个字典

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'])

有人可以帮我解决吗?

标签: algorithmsubsequence

解决方案


这是问题的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])

推荐阅读