gpt4 book ai didi

python - 如何将字典列表中的值与输入值进行比较

转载 作者:行者123 更新时间:2023-12-01 00:19:25 25 4
gpt4 key购买 nike

我现在正在尝试制作一个程序,用于匹配合适的移民国家。我在运行时发现了一些问题。

以下是系统所需的一些演示数据:

data = [{'Country': 'Taiwan','Age': 18, 'Status': 'False', 'Education level': 1}, 
{'Country': 'Japan','Age': 30, 'Status': 'True', 'Education level': 8},
{'Country': 'Korea','Age': 20, 'Status': 'False', 'Education level': 6}]
ans = ["yes", "no", "Y", "N", "y", "n", "Yes", "No"]
ans_y = ["yes", "Y", "y", "Yes"]
ans_n = ["no", "N", "n", "No"]

然后我开始通过一些错误处理来询问用户信息。

def collecting_info():
age = 0
while True:
try:
age = int(input('\nWhat is your age?'))
break
except ValueError:
print("Please insert valid input.")
continue

while True:
status = str(input('Are you married?'))
if status not in ans:
print("Please insert the valid input.)")
continue
else:
break

print("\nYou are {} year-old".format(age))

# print status
if status in ans:
if status in ans_y:
status = 'True'
print("You are Married.")

elif status in ans_n:
status = 'False'
print("You are not Married.")
return status, age
age_match()

之后,我开始进行匹配,我的想法是使用for...loop删除不匹配的dictionary并在最后打印最合适的地方。

# matching age
def age_match():
age = collecting_info()
for dict_age in data:
for age_dict in dict_age:
ad = iter(age_dict)
if ad == 'Age':
if age_dict.value > age:
del data[dict_age]


# matching marriage
def status_match():
status = collecting_info()
for dict_status in data:
for status_dict in dict_status:
sd = iter(status_dict)
if sd == str('Status\n(tick = M)'):
if status_dict.value == status:
del data[dict_status]
print(data)

但是,程序只返回Age和Status的值……(我的输入--- Age: 20; Status: no)

You are 20 year-old
You are not Married.

我的预期输出是

[{'Country': 'Korea','Age': 20, 'Status': 'False', 'Education level': 6}]

这是我第一次自己做程序,希望能得到一些帮助。

最佳答案

我认为在循环时修改列表不是一个好主意

data = [{'Country': 'Taiwan','Age': 18, 'Status': 'False', 'Education level': 1},
{'Country': 'Japan','Age': 30, 'Status': 'True', 'Education level': 8},
{'Country': 'Korea','Age': 20, 'Status': 'False', 'Education level': 6}]
ans = ["yes", "no", "Y", "N", "y", "n", "Yes", "No"]
ans_y = ["yes", "Y", "y", "Yes"]
ans_n = ["no", "N", "n", "No"]

def collecting_info():
age = 0
while True:
try:
age = int(input('\nWhat is your age?'))
break
except ValueError:
print("Please insert valid input.")
continue

while True:
status = str(input('Are you married?'))
if status not in ans:
print("Please insert the valid input.)")
continue
else:
break

print("\nYou are {} year-old".format(age))

# print status
if status in ans:
if status in ans_y:
status = 'True'
print("You are Married.")

elif status in ans_n:
status = 'False'
print("You are not Married.")
return age, status

# matching age
def age_match(age):
data_copy = []
for people in data:
if people['Age'] == age:
data_copy.append(people)
return data_copy


# matching marriage
def status_match(status):
data_copy = []
for people in data:
if people['Status'] == status:
data_copy.append(people)
return data_copy

age, status = collecting_info()
age_group = age_match(age)
status_group = status_match(status)
for p in age_group:
if p in status_group:
print(p)

关于python - 如何将字典列表中的值与输入值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59055289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com