列表中的字典
Dictionary inside List
假设我有这个列表:
a=[ {"user":"John","password":"123"} , {"user":"Mike","password":"12345"} ,{"user":"Peter","password":"qwerty"}]
user=input("insert your username")
password=input("insert your password")
现在我想知道输入的用户名和密码是否在之前的列表中,如何解决这个问题?
如果我现在想要有 3 种情况:一种是用户和密码匹配,第二种是用户名正确但密码不正确,最后一种是用户名不存在。
if {'user':user,'password':password} in a:
print("okay")
else:
if {'user':user} in a and {'password':password} not in a:
print("user okay, but incorrect pass")
else:
print("No username")
这种代码行不通,对吧?
那么如何解决第二步(在第一步之后)?
使用:
if {'user':user,'password':password} in a:
# it is already in there
else:
# it isn't in there
编辑
使用:
if {'user':user,'password':password} in a:
# it is already in there
elif any(user == i['user'] and password != i['password'] for i in a):
# user name is correct, but password isn't
else:
# it isn't in there
一种方法:
a=[ {"user":"John","password":"123"} , {"user":"Mike","password":"12345"} ,{"user":"Peter","password":"qwerty"}]
user=input("insert your username")
password=input("insert your password")
flag = True
for i in a :
if i['user'] == user and i['password'] == password :
print('match') # or whatever
flag = False
break
if flag :
print('not there ') # or whatever
您可以遵循@U9-Forward 建议的解决方案,或者您可以更改您的字典,而不是拥有字典列表,您可以简单地拥有一个字典,其中键 = 实际名称和值 = 实际密码。如果你的 dict 中有很多对象,并且这个函数将被经常调用,那么这个解决方案在时间复杂度方面更好。
因此,您最初将列表 a 转换为名为 user_password_dict
的字典
user_password_dict = {user_obj['user']: user_obj['password'] for user_obj in a}
之后,您可以通过以下语句轻松检查您的 user_password_dict
中是否存在用户和相应的密码:
if user in user_password_dict and user_password_dict[user] == password:
# do something here
else:
# do something here
假设我有这个列表:
a=[ {"user":"John","password":"123"} , {"user":"Mike","password":"12345"} ,{"user":"Peter","password":"qwerty"}]
user=input("insert your username")
password=input("insert your password")
现在我想知道输入的用户名和密码是否在之前的列表中,如何解决这个问题?
如果我现在想要有 3 种情况:一种是用户和密码匹配,第二种是用户名正确但密码不正确,最后一种是用户名不存在。
if {'user':user,'password':password} in a:
print("okay")
else:
if {'user':user} in a and {'password':password} not in a:
print("user okay, but incorrect pass")
else:
print("No username")
这种代码行不通,对吧? 那么如何解决第二步(在第一步之后)?
使用:
if {'user':user,'password':password} in a:
# it is already in there
else:
# it isn't in there
编辑
使用:
if {'user':user,'password':password} in a:
# it is already in there
elif any(user == i['user'] and password != i['password'] for i in a):
# user name is correct, but password isn't
else:
# it isn't in there
一种方法:
a=[ {"user":"John","password":"123"} , {"user":"Mike","password":"12345"} ,{"user":"Peter","password":"qwerty"}]
user=input("insert your username")
password=input("insert your password")
flag = True
for i in a :
if i['user'] == user and i['password'] == password :
print('match') # or whatever
flag = False
break
if flag :
print('not there ') # or whatever
您可以遵循@U9-Forward 建议的解决方案,或者您可以更改您的字典,而不是拥有字典列表,您可以简单地拥有一个字典,其中键 = 实际名称和值 = 实际密码。如果你的 dict 中有很多对象,并且这个函数将被经常调用,那么这个解决方案在时间复杂度方面更好。
因此,您最初将列表 a 转换为名为 user_password_dict
user_password_dict = {user_obj['user']: user_obj['password'] for user_obj in a}
之后,您可以通过以下语句轻松检查您的 user_password_dict
中是否存在用户和相应的密码:
if user in user_password_dict and user_password_dict[user] == password:
# do something here
else:
# do something here