获取嵌套可迭代的特定键,并检查其值是否在列表中

问题描述 投票:0回答:1

我正在尝试访问嵌套词典中的特定键,然后将其值与列表中的字符串匹配。如果列表中的字符串包含字典值中的字符串,我想用列表值覆盖字典值。下面是一个示例。

my_list = ['string1~', 'string2~', 'string3~', 'string4~', 'string5~', 'string6~']
my_iterable = {'A':'xyz', 
               'B':'string6', 
               'C':[{'B':'string4', 'D':'123'}], 
               'E':[{'F':'321', 'B':'string1'}], 
               'G':'jkl'
               'B':'string99'}

我正在寻找的键是B,目标是用string6覆盖string6~,用string4覆盖string4~,依此类推,对于B中找到的所有my_iterable键。

[我编写了一个函数来计算两个字符串之间的Levenshtein距离,但是我正在努力写出一种有效的方法来覆盖键的值。

def find_and_replace(key, dictionary, original_list):
    for k, v in dictionary.items():
        if k == key:
            #function to check if original_list item contains v
            yield v
        elif isinstance(v, dict):
            for result in find_and_replace(key, v, name_list):
                yield result
        elif isinstance(v, list):
            for d in v:
                if isinstance(d, dict):
                    for result in find_and_replace(key, d, name_list):
                        yield result

如果我打电话

updated_dict = find_and_replace('B', my_iterable, my_list)

我希望updated_dict返回以下内容:

{'A':'xyz', 
 'B':'string6~', 
 'C':[{'B':'string4~', 'D':'123'}], 
 'E':[{'F':'321', 'B':'string1~'}], 
 'G':'jkl',
 'B': 'string99'
}

这是最有效的解决方案的正确方法,如何修改它以返回包含B的更新值的字典?

python loops dictionary string-matching
1个回答
0
投票
# Input List my_list = ['string1~', 'string2~', 'string3~', 'string4~', 'string5~', 'string6~'] # Input Dict # Removed duplicate key "B" from the dict my_iterable = {'A':'xyz', 'B':'string6', 'C':[{'B':'string4', 'D':'123'}], 'E':[{'F':'321', 'B':'string1'}], 'G':'jkl', } # setting search key search_key = "B" # Main code for i, v in my_iterable.items(): if i == search_key: if not isinstance(v,list): search_in_list = [i for i in my_list if v in i] if search_in_list: my_iterable[i] = search_in_list[0] else: try: for j, k in v[0].items(): if j == search_key: search_in_list = [l for l in my_list if k in l] if search_in_list: v[0][j] = search_in_list[0] except: continue # print output print (my_iterable) # Result -> {'A': 'xyz', 'B': 'string6~', 'C': [{'B': 'string4~', 'D': '123'}], 'E': [{'F': '321', 'B': 'string1~'}], 'G': 'jkl'}

以上可以使用列表理解或使用来优化范围功能
我希望这对您有所帮助!
© www.soinside.com 2019 - 2024. All rights reserved.