检查整个列表中是否存在键或python中的字典 [英] Check a key exist in entire list or dict in python

查看:105
本文介绍了检查整个列表中是否存在键或python中的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用python创建字典,这个字典可能包括列表和子字典.如果我想先检查某个字典中是否存在某个密钥,那么我就可以到达该字典,然后只有我可以检查该密钥是否存在.但是有什么方法可以使用父级dict检查密钥是否存在,仅意味着如果父级dict有任何子dict,然后搜索到每个级别,让我知道密钥是否存在于整个dict中,然后使用该密钥,我可以在包含该键的字典中插入或删除元素.

I am creating dict in python and this dict may includes lists and sub dicts . If I want to check a key exist in any dict first I have reach to that dict and then only I can check wether the key exists or not . But is there any way that I can check key existence using parent dict only means if parent dict have any child dicts then searching goes to every level and let me know that wether the key exist in entire dict or not and then using that key I can insert or delete elments in that dict which includes that key.

例如,我有这个

[
    {
        'groupName': u'MainMenu_1',
        'menuItems': [
            {
                'action': u'/home\r\n',
                'sub_1': [

                ],
                'id': 1L,
                'name': u'Home\r\n'
            },
            {
                'action': u'/inventory',
                'sub_2': [

                ],
                'id': 2L,
                'name': u'Inventory\r\n'
            },
            {
                'action': u'/accounting\r\n',
                'sub_3': [
                    {
                        'action': u'/gl\r\n',
                        'name': u'GL\r\n',
                        'sub_4': [

                        ]
                    },
                    {
                        'action': u'/ap\r\n',
                        'name': u'AP\r\n',
                        'sub_5': [

                        ]
                    },
                    {
                        'action': u'/ar\r\n',
                        'sub_6': [

                        ],
                        'name': u'AR\r\n'
                    }
                ],
                'id': 3L,
                'name': u'Accounting\r\n'
            },
            {
                'action': u'/crm\r\n',
                'sub_8': [

                ],
                'id': 8L,
                'name': u'CRM\r\n'
            }
        ]
    },
    {
        'groupName': u'MainMenu_2',
        'menuItems': [
            {
                'action': u'/iv-receive\r\n',
                'sub_9': [

                ],
                'id': 9L,
                'name': u'Receiving\r\n'
            },
            {
                'action': u'/iv-shipping\r\n',
                'sub_10': [

                ],
                'id': 10L,
                'name': u'Shipping\r\n'
            }
        ]
    }
]

现在,如果在上面的示例中我想搜索诸如sub_1,sub_3,sub_6之类的任何键,那么我如何搜索该键

Now if in above example I want to search for any key like sub_1 , sub_3, sub_6 then how I can search for this key

推荐答案

我们可以递归搜索所有符合条件的词典.以下实现将此类词典的所有引用附加到列表found:

We can search for all qualifying dictionary recursively. The following implementation appends all references of such dictionaries to the list found:

def recursive_search(items, key):
    found = []
    for item in items:
        if isinstance(item, list):
            found += recursive_search(item, key)
        elif isinstance(item, dict):
            if key in item:
                found.append(item)
            found += recursive_search(item.values(), key)
    return found

found = recursive_search(items, 'sub_9')

这篇关于检查整个列表中是否存在键或python中的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆