使用用户的输入从嵌套字典中检索字典列表
Retrieving a list of Dictionaries from Nested Dictionary with an Input from User
我对 Python 有点陌生,我正在使用 Python 3. 我想要做的是从嵌套字典中检索字典列表,其中包含与用户将输入的内容类似的密钥。
这是我正在做的事情
import pickle
import os
os.system("cls")
p_inputs = {
'DOE, JOHN': {'SEX': 'MALE', 'AGE': 18},
'DOE, JANE': {'SEX': 'FEMALE', 'AGE': 18},
'WEST, MARIA': {'SEX': 'FEMALE', 'AGE': 18}}
print("Please enter the necessary information.")
new_name = input("\nFULL NAME, SURNAME FIRST: ").upper()
new_sex = input("SEX: ").upper()
new_age = int(input("AGE: "))
p_inputs[new_name] = {'SEX': new_sex, 'AGE': new_age,}
pickle.dump(p_inputs, open("info", "wb"))
现在,当我插入一行要求用户输入时,我应该怎么做,例如,我要求输入年龄。然后用户将输入“18”,我如何获得所有年龄为 18 岁的人的词典列表?
如有不妥,敬请见谅。
首先你需要像这样加载现有数据:
with open("info", "rb") as f:
p_inputs = pickle.load(f)
然后你可以做一个字典理解:
>>> {k: v for k,v in p_inputs.items() if v["AGE"] == 18}
{'name2': {'SEX': 'sex1', 'AGE': 18}, 'name3': {'SEX': 'sex2', 'AGE': 18}}
顺便说一句:目前你每次都会覆盖字典并只存储最新的人。
编辑:完整解决方案
import os
import pickle
os.system("cls")
try:
with open("info", "rb") as f:
p_inputs = pickle.load(f)
except FileNotFoundError:
p_inputs = {}
print("Please enter the necessary information.")
new_name = input("\nFULL NAME, SURNAME FIRST: ").upper()
new_sex = input("SEX: ").upper()
new_age = int(input("AGE: "))
p_inputs[new_name] = {'SEX': new_sex, 'AGE': new_age}
with open("info", "wb") as f:
pickle.dump(p_inputs, f)
print("Added")
print()
search_age = int(input("Show people with this age: "))
search_result = {k: v for k, v in p_inputs.items() if v["AGE"] == search_age}
print(search_result)
Input/Output
Please enter the necessary information.
FULL NAME, SURNAME FIRST: DOE, JOHN
SEX: MALE
AGE: 18
Added
Show people with this age: 18
{'DOE, JANE': {'SEX': 'FEMALE', 'AGE': 18}, 'DOE, JOHN': {'SEX': 'MALE', 'AGE': 18}, 'WEST, MARIA': {'SEX': 'FEMALE', 'AGE'
: 18}}
我对 Python 有点陌生,我正在使用 Python 3. 我想要做的是从嵌套字典中检索字典列表,其中包含与用户将输入的内容类似的密钥。
这是我正在做的事情
import pickle
import os
os.system("cls")
p_inputs = {
'DOE, JOHN': {'SEX': 'MALE', 'AGE': 18},
'DOE, JANE': {'SEX': 'FEMALE', 'AGE': 18},
'WEST, MARIA': {'SEX': 'FEMALE', 'AGE': 18}}
print("Please enter the necessary information.")
new_name = input("\nFULL NAME, SURNAME FIRST: ").upper()
new_sex = input("SEX: ").upper()
new_age = int(input("AGE: "))
p_inputs[new_name] = {'SEX': new_sex, 'AGE': new_age,}
pickle.dump(p_inputs, open("info", "wb"))
现在,当我插入一行要求用户输入时,我应该怎么做,例如,我要求输入年龄。然后用户将输入“18”,我如何获得所有年龄为 18 岁的人的词典列表?
如有不妥,敬请见谅。
首先你需要像这样加载现有数据:
with open("info", "rb") as f:
p_inputs = pickle.load(f)
然后你可以做一个字典理解:
>>> {k: v for k,v in p_inputs.items() if v["AGE"] == 18}
{'name2': {'SEX': 'sex1', 'AGE': 18}, 'name3': {'SEX': 'sex2', 'AGE': 18}}
顺便说一句:目前你每次都会覆盖字典并只存储最新的人。
编辑:完整解决方案
import os
import pickle
os.system("cls")
try:
with open("info", "rb") as f:
p_inputs = pickle.load(f)
except FileNotFoundError:
p_inputs = {}
print("Please enter the necessary information.")
new_name = input("\nFULL NAME, SURNAME FIRST: ").upper()
new_sex = input("SEX: ").upper()
new_age = int(input("AGE: "))
p_inputs[new_name] = {'SEX': new_sex, 'AGE': new_age}
with open("info", "wb") as f:
pickle.dump(p_inputs, f)
print("Added")
print()
search_age = int(input("Show people with this age: "))
search_result = {k: v for k, v in p_inputs.items() if v["AGE"] == search_age}
print(search_result)
Input/Output
Please enter the necessary information.
FULL NAME, SURNAME FIRST: DOE, JOHN
SEX: MALE
AGE: 18
Added
Show people with this age: 18
{'DOE, JANE': {'SEX': 'FEMALE', 'AGE': 18}, 'DOE, JOHN': {'SEX': 'MALE', 'AGE': 18}, 'WEST, MARIA': {'SEX': 'FEMALE', 'AGE'
: 18}}