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

时间:2019-11-26 16:23:14

标签: python

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

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

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"]

然后我开始用一些Error Handling询问用户的信息。

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}]

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

2 个答案:

答案 0 :(得分:1)

使用:

age, status = collecting_info()
result = [d for d in data if d['Age'] == age and d['Status'] == status]

代替age_matchstatus_match

答案 1 :(得分:1)

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

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)