比较 Python 3 中字典中的值不起作用?
Comparing values in dictionary in Python 3 not working?
我一直在努力弄清楚如何在我的 Python 课程的作业中访问(和比较)字典中的值。
我认为我对键和值的理解足够好,但出于某种原因,无论我尝试比较值,我的程序似乎永远不会看到重复值。
为什么这不起作用?
if new_value in b_dict.values():
print("Password already exists...\n")
else:
add_to_dict(b_dict, new_key, new_value)
print("\nPassword added successfully.\n")
完整节目:
def add_to_dict(dict, key, value):
if key in dict:
dict[key].append(value)
else:
dict[key] = [value]
if __name__ == "__main__":
b_dict = {"att": ["12345"]}
new_key = input("Please enter an organization ID to add: ")
if new_key in b_dict:
print("Organization exists, adding to this organization...")
if new_key not in b_dict:
print("Organization does not exist, adding it...")
new_value = input("Please input a password: ")
print("Adding password...")
if new_value in b_dict.values():
print("Password already exists...\n")
else:
add_to_dict(b_dict, new_key, new_value)
print("\nPassword added successfully.\n")
print(b_dict)
我希望当用户输入 att,然后输入 12345 时,程序会通知他们密码已经存在,然后打印原始字典。相反,它总是打印具有重复 12345 值的字典。
b_dict.values()
将包含 b_dict
所有键的所有值 - 这意味着它将包含 ['12345']
但不包含 '12345'
- 我认为你需要什么检查是
if [new_value] in b_dict.values()
但这适用于这种特殊情况,并且当您附加新值时,您可能需要分别遍历所有键或展平值
这段代码是否解决了您的问题?
if key in dict:
dict[key].append(value)
else:
dict[key] = [value]
def check_values(value,key) :
if key in b_dict :
return value in b_dict[key]
else :
return False
if __name__ == "__main__":
b_dict = {"att": ["12345"]}
new_key = input("Please enter an organization ID to add: ")
new_value = input("Please input a password: ")
if check_values(new_value,new_key) == True :
print("They already exist..")
elif check_values(new_value,new_key) == False :
print("They are not exits.. adding it")
if new_key in b_dict :
print("this Organization's password already exist")
else :
print("adding new organization and password")
add_to_dict(b_dict, new_key, new_value)
print(b_dict)
我一直在努力弄清楚如何在我的 Python 课程的作业中访问(和比较)字典中的值。
我认为我对键和值的理解足够好,但出于某种原因,无论我尝试比较值,我的程序似乎永远不会看到重复值。
为什么这不起作用?
if new_value in b_dict.values():
print("Password already exists...\n")
else:
add_to_dict(b_dict, new_key, new_value)
print("\nPassword added successfully.\n")
完整节目:
def add_to_dict(dict, key, value):
if key in dict:
dict[key].append(value)
else:
dict[key] = [value]
if __name__ == "__main__":
b_dict = {"att": ["12345"]}
new_key = input("Please enter an organization ID to add: ")
if new_key in b_dict:
print("Organization exists, adding to this organization...")
if new_key not in b_dict:
print("Organization does not exist, adding it...")
new_value = input("Please input a password: ")
print("Adding password...")
if new_value in b_dict.values():
print("Password already exists...\n")
else:
add_to_dict(b_dict, new_key, new_value)
print("\nPassword added successfully.\n")
print(b_dict)
我希望当用户输入 att,然后输入 12345 时,程序会通知他们密码已经存在,然后打印原始字典。相反,它总是打印具有重复 12345 值的字典。
b_dict.values()
将包含 b_dict
所有键的所有值 - 这意味着它将包含 ['12345']
但不包含 '12345'
- 我认为你需要什么检查是
if [new_value] in b_dict.values()
但这适用于这种特殊情况,并且当您附加新值时,您可能需要分别遍历所有键或展平值
这段代码是否解决了您的问题?
if key in dict:
dict[key].append(value)
else:
dict[key] = [value]
def check_values(value,key) :
if key in b_dict :
return value in b_dict[key]
else :
return False
if __name__ == "__main__":
b_dict = {"att": ["12345"]}
new_key = input("Please enter an organization ID to add: ")
new_value = input("Please input a password: ")
if check_values(new_value,new_key) == True :
print("They already exist..")
elif check_values(new_value,new_key) == False :
print("They are not exits.. adding it")
if new_key in b_dict :
print("this Organization's password already exist")
else :
print("adding new organization and password")
add_to_dict(b_dict, new_key, new_value)
print(b_dict)