简体   繁体   English

如果字典列表中的字典中存在另一个值,如何返回一个值?

[英]How to return a value if another value exists in a dict in a list of dicts?

How can I check if a specific "type" value exists in a dict in group_list ?如何检查group_list中的字典中group_list存在特定的"type"值? If it does, I want to return that dict's "app" value.如果是这样,我想返回该 dict 的"app"值。

group_list = [
    {
        'type': "app_group1",
        'app': ['vs code', 'notepad', 'google']
    },
    {
        'type': 'app_group2',
        'app': ['slack', 'Discord', 'zoom', 'vs code']
    },
    {
        'type': 'app_group3',
        'app': ['calculater', 'google']
    }]

You may iterate on each dict to verify the type and return the app is that's the one您可以迭代每个dict以验证type并返回app就是那个

def get_app(groups, app_type):
    for group in groups:
        if group['type'] == app_type:
            return group['app']
    return None



group_list = [{'type': "app_group1", 'app': ['vs code', 'notepad', 'google', ]},
              {'type': 'app_group2', 'app': ['slack', ' Discord', 'zoom', 'vs code', ]},
              {'type': 'app_group3', 'app': ['calculater', 'google']}]

print(get_app(group_list, 'app_group1'))  # ['vs code', 'notepad', 'google']
print(get_app(group_list, 'app_group5'))  # None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从字典列表中的字典中获取值 - How to get a value from a dict in a list of dicts 使用__str__从列表返回键和值dict - Return key and value dict from list with dicts using __str__ 如何在一个dicts列表中获得具有公共密钥最大值的整个dict - How to get whole dict with max value of a common key in a list of dicts python:如何根据值合并字典列表中的字典 - python: how to merge dict in list of dicts based on value 如何按预定义格式按字典值对字典列表进行排序? - How to sort list of dicts by dict value in pre-defined format? 如何计算字典列表中特定字典键的出现次数,一些字典值包含列表和 append 计数值 - How to count occurrences of a specific dict key in dicts list and some dicts values ​contains list and append the count in value 通过在 Python 中 dict 的 dict 列表中搜索另一个值来返回一个值 - Return A Value by searching for another value in a list of dict of dict in Python 从 dicts 的 dict 创建一个内部值列表 - Create a list of an inner value from a dict of dicts 在字典列表中,如果键/值对的组合在另一个字典中相同,则标记一个字典 - In a list of dicts, flag a dict if combination of key/value pairs is identical in another dict 如何从字典列表中通过字典值y获取字典值x - How to get dict value x via dict value y from list of dicts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM