Python:如果字典中的较长键中存在该值,则从字典值列表中删除值

时间:2019-12-09 11:06:45

标签: python

我有一个这样的字典:

{'key1':['val1','val2','val3'], 'key1_key2':['val1','val3','val4']}

我要删除冗余,对于每个值,如果该值也是另一个带有较长单词的键的值,请从较短的单词键中删除该值。

因此在此示例中,输出为:

{'key1':['val2'],'key1_key2':['val1','val3','val4']}

我正在寻找最Python化的方式来做到这一点。我知道我可以开发的一种方法是:

#for each key and value in the dict
for k,v in dict1.items():
        #so i can compare to every other key and value in the dict
        for k2,v2 in dict1.items():
                #if k2 string is a subset of k string (i.e. if one k is 'key1' and one k is 'key1_key2'):
                        small_list = #remove any item that's also in the big list
                        dict1[k] = small_list

我想知道这是开发此内容的最佳方法(在我被拖累之前无法尝试工作)还是有人有更好的解决方案。

1 个答案:

答案 0 :(得分:-1)

您可以执行以下操作:

from collections import defaultdict

d = {'key1': ['val1', 'val2', 'val3'], 'key1_key2': ['val1', 'val3', 'val4']}

longest_key = defaultdict(str)

# create a dictionary that maps values to the longest key
for key, values in d.items():
    for value in values:
        longest_key[value] = max(longest_key[value], key, key=len)

# filter out the input dictionary using the lookup dictionary created in the above step
for key, values in d.items():
    d[key] = [value for value in values if longest_key[value] == key]

print(d)

输出

{'key1': ['val2'], 'key1_key2': ['val1', 'val3', 'val4']}