[英]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.