如何檢查"type"dict 中group_list是否存在特定值?如果是這樣,我想回傳該字典的"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']
}]
uj5u.com熱心網友回復:
您可以迭代每個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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/352453.html
上一篇:python中兩個串列的乘積
下一篇:我應該使用熊貓字典嗎?如何?
