在嵌套字典中搜索值列表

社会演员多 nlp 201

原文标题Search for a list of values in a nested dictionary

所以我正在尝试使用值列表对嵌套字典进行搜索。我的嵌套字典包含每个键的有关不同产品的信息(对于每个键,我都有一个唯一的产品)。

我试图从我的字典中获取那些包含我列表中这三个单词的键。

这个想法是,这个搜索可以灵活地改变列表添加新单词(所以我的列表的长度可以改变)。

例如:

我的价值观清单是:

details = ['PARACETAMOL','100','MG']

我有下一本字典:

dict = {{
    1:  'description': 'KITADOL  500  TABL  1 . 00 G x  18 ',
        'key_words': ['KITADOL', '500', 'TABL', '1', '.', '00', 'G', 'x', '18'],
        'molecule': ['PARACETAMOL']},
    {
    2:  'description': 'ACAMOL GOTAS  100 MG  15 ML x  1  /ML',
        'key_words': ['ACAMOL', 'GOTAS', '100', 'MG', '15', 'ML', 'x', '1', '/ML'],
        'molecule': ['PARACETAMOL']},
    {   
    3:  'description': 'PANAGESIC GOTAS  100 MG  15 ML x  1 ',
        'key_words': ['PANAGESIC', 'GOTAS', '100', 'MG', '15', 'ML', 'x', '1'],
        'molecule': ['PARACETAMOL']},
    {
    4:  'description': 'GRIPASAN DIA Y NOC CA.D 12 +CA.N 6  x  18 ',
        'key_words': ['GRIPASAN','DIA','Y','NOC','CA.D','12','+CA.N','6','x','18'],
        'molecule': ['CAFEINA','CLORFENAMINA','PARACETAMOL','PROPIFENAZONA','PSEUDOEFEDRINA']}
    }

所以我想得到下一个键34

我想知道是否有人可以帮助我或提供一些想法。谢谢!

原文链接:https://stackoverflow.com//questions/71434843/search-for-a-list-of-values-in-a-nested-dictionary

回复

我来回复
  • Amir Shamsi的头像
    Amir Shamsi 评论

    我想它可能会帮助您找到您需要的keys

    除了

    • 您的 dict 指令在打开括号 { 时遇到了一些问题。
    • 你提到的结果不是正确的答案

    搜索功能:

    def dict_custom_search(dictt, details):
        keys = []
        for index in range(1, len(dictt)):
            found_counter = 0
            for detail in details:
                if detail in dictt[index]['description'] or detail in dictt[index]['key_words'] or detail in dictt[index]['molecule']:
                    found_counter += 1
            if found_counter == len(details):
                keys.append(index)
        return keys
    

    输入

    dictf = {
        1:  {'description': 'KITADOL  500  TABL  1 . 00 G x  18 ',
             'key_words': ['KITADOL', '500', 'TABL', '1', '.', '00', 'G', 'x', '18'],
             'molecule': ['PARACETAMOL']},
    
        2: { 'description': 'ACAMOL GOTAS  100 MG  15 ML x  1  /ML',
             'key_words': ['ACAMOL', 'GOTAS', '100', 'MG', '15', 'ML', 'x', '1', '/ML'],
             'molecule': ['PARACETAMOL']},
    
        3: { 'description': 'PANAGESIC GOTAS  100 MG  15 ML x  1 ',
             'key_words': ['PANAGESIC', 'GOTAS', '100', 'MG', '15', 'ML', 'x', '1'],
             'molecule': ['PARACETAMOL']},
    
        4:  {'description': 'GRIPASAN DIA Y NOC CA.D 12 +CA.N 6  x  18 ',
             'key_words': ['GRIPASAN','DIA','Y','NOC','CA.D','12','+CA.N','6','x','18'],
             'molecule': ['CAFEINA','CLORFENAMINA','PARACETAMOL','PROPIFENAZONA','PSEUDOEFEDRINA']}
    }
    
    details = ['PARACETAMOL','100','MG']
    
    

    在调用函数dict_custom_search(dictf, details)并传递参数后,您将获得找到的键列表。

    输出

    [2, 3]
    
    2年前 0条评论
  • baskettaz的头像
    baskettaz 评论

    你可以尝试这样的事情:

    details = ['PARACETAMOL','100','MG']
    [k for k,v in d.items() if all(map(lambda x: x in v["key_words"] + v["molecule"],details))]
    

    结果:

    [2,3]
    

    *注意力: *

    1. 永远不要使用 dict 作为变量,这是一个保留字
    2. 你的结构不好“{”-一开始就太笨拙了
    2年前 0条评论