我有以下清单,
admin_extra = [
{
'name': 'nikhil',
'passkey': 'nikhilpasskey'
},
{
'name': 'mac',
'passkey': 'macpasskey'
},
]
如何以更好的方式在列表中获取字典?如果找不到匹配项,则打印错误?
我已经完成了
name = 'nikhil'
flag = 0
for admin in admin_extra:
if admin['name'] == name:
passkey = admin[passkey]
flag = 1
return passkey
if not flag:
print "not found"
我也希望消除标志逻辑