首页 > 解决方案 > 缺少对键列表的检查:值元素

问题描述

我编写了一个代码,该代码应检查输入中每个包(包 - 一组键:由空行分隔的值项)中的所有字段是否存在并包含正确的值。这是我的输入(result2.txt):

byr:1983
iyr:2017
pid:796082981
eyr:2030
ecl:oth
hgt:182cm

iyr:2019
eyr:2039
hcl:#cfa07d
hgt:171cm
ecl:#0180ce
byr:2006
pid:8204115568

byr:1991
eyr:2022
hcl:#341e13
iyr:2016
pid:729933757
hgt:167cm
ecl:gry

hcl:231d64
ecl:gmt
eyr:2039
hgt:189in
pid:#9c3ea1

ecl:#1f58f9
pid:#758e59
iyr:2022
hcl:z
byr:2016
hgt:68
eyr:1933

等等+250包

以下是对值和字段的要求:

The expected fields are as follows:

byr (Birth Year)
iyr (Issue Year)
eyr (Expiration Year)
hgt (Height)
hcl (Hair Color)
ecl (Eye Color)
pid (Passport ID)
cid (Country ID) - ignored, missing or not.

取值要求如下: 自动验证有效:

byr (Birth Year) - four digits; at least 1920 and at most 2002.
iyr (Issue Year) - four digits; at least 2010 and at most 2020.
eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
hgt (Height) - a number followed by either cm or in:
If cm, the number must be at least 150 and at most 193.
If in, the number must be at least 59 and at most 76.
hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.
pid (Passport ID) - a nine-digit number, including leading zeroes.
cid (Country ID) - ignored, missing or not.

这是我的代码:

def check_fields(list): #list = ['byr:1983', 'iyr:2017', 'pid:796082981', 'cid:129', 'eyr:2030', 'ecl:oth', 'hgt:182cm']
    list = [i[:3] for i in list]
    comparison_list = ['byr', 'iyr', 'eyr',
                       'hgt', 'hcl', 'ecl',
                       'pid']
    statement = True
    for i in comparison_list:
        statement = statement and (i in list)
    return statement


def check_byr_iyr_eyr(line):
    prefix,value = line.split(':')
    cases = {'byr':{'min':1920, 'max':2002},
             'iyr':{'min':2010, 'max':2020},
             'eyr':{'min':2020, 'max':2030} }
    return cases[prefix]['min'] <= int(value) <= cases[prefix]['max']


def check_hgt(line):
    unit = line[len(line)-2] + line[len(line)-1]
    if unit != 'cm' or unit != 'in':
        return False
    value = line[line.index(':')+1: -2]
    cases = {'cm':{'min':150, 'max':193},
             'in':{'min':59, 'max':76}}
    return cases[unit]['min'] <= int(value) <= cases[unit]['max']


def check_hcl(line):
    statement = True
    if line[line.index(':')+1] != '#' or len(line[line.index(':')+2:]) != 6:
        return False
    else:
        string = line[line.index('#')+1:]
        for i in string:
            statement = statement and (97 <= ord(i) <= 102 or 48 <= ord(i) <= 57)
        return statement


def check_ecl(line):
    comparison_list = ['amb', 'blu', 'brn',
                       'gry', 'grn', 'hzl',
                       'oth' ]
    if line[line.index(':') + 1:] in comparison_list:
        return True
    return False


def check_pid(line):
    if len(line[line.index(':')+1:]) != 9:
        return False
    try:
        int(line[line.index(':')+1:])
        return True
    except:
        return False

''' below '''

line_list = []
valid_passports = 0
function_pool = {'ecl':check_ecl,'byr':check_byr_iyr_eyr, 'iyr':check_byr_iyr_eyr,
                 'hgt':check_hgt, 'hcl':check_hcl, 'eyr':check_byr_iyr_eyr, 'pid':check_pid}
statement = True

with open('results2.txt', 'r') as f:
    for line in f:
        if line != '\n':
            line_list.append(line.strip('\n'))
        else:
            print(line_list)
            if check_fields(line_list):
                for i in line_list:
                    print('prefix - i[:3]:', i[:3])
                    checking_function = function_pool[i[:3]]
                    statement = statement and checking_function(i)
                if statement:
                    print('added +1, statement == True')
                    valid_passports += 1
                else:
                    statement = True
            line_list.clear()



print(valid_passports) 

问题是我得到这样的输出:

['hgt:177cm', 'byr:1954', 'ecl:amb', 'pid:445374119', 'hcl:#341e13', 'iyr:2010', 'eyr:2020']
prefix - i[:3]: hgt
prefix - i[:3]: byr
prefix - i[:3]: ecl
prefix - i[:3]: pid
prefix - i[:3]: hcl
prefix - i[:3]: iyr
prefix - i[:3]: eyr
['hgt:160cm', 'byr:1923', 'ecl:grn', 'eyr:2021', 'iyr:2012', 'pid:286304911', 'hcl:#18171d']
prefix - i[:3]: hgt
prefix - i[:3]: byr
prefix - i[:3]: ecl
prefix - i[:3]: eyr
prefix - i[:3]: iyr
prefix - i[:3]: pid
prefix - i[:3]: hcl
['hgt:153cm', 'byr:1933', 'iyr:2015', 'ecl:gry', 'pid:365430749', 'eyr:2029']
['eyr:2030', 'ecl:oth', 'iyr:2014', 'hgt:181cm', 'hcl:#623a2f']
['iyr:2011', 'ecl:gry', 'hgt:177cm', 'eyr:2025', 'pid:446342686', 'hcl:#b6652a', 'byr:1991']
prefix - i[:3]: iyr
prefix - i[:3]: ecl
prefix - i[:3]: hgt
prefix - i[:3]: eyr
prefix - i[:3]: pid
prefix - i[:3]: hcl
prefix - i[:3]: byr
['byr:1999', 'iyr:2018', 'hgt:188cm', 'ecl:gry', 'pid:473752814']
['byr:2002', 'hcl:#733820', 'pid:867697169', 'ecl:gry', 'hgt:165cm', 'eyr:2020']
['eyr:2026', 'hgt:175cm', 'byr:1993', 'pid:531385722', 'ecl:hzl', 'hcl:#733820']
['eyr:2027', 'byr:1969', 'iyr:2011', 'ecl:hzl', 'hgt:164cm', 'hcl:#b6652a']
['eyr:2020', 'ecl:gry', 'hgt:186cm', 'pid:917147781', 'hcl:#341e13', 'iyr:2016']
['pid:857547233', 'hgt:64in', 'eyr:2020', 'ecl:hzl', 'iyr:2019', 'hcl:#866857', 'byr:1948']

这意味着不是每个 key:value 元素列表都经过正确性检查(查看最后 6 行输出 - 它们粘在一起),另一个问题是valid_passports根本没有增加。奇怪的是,当我从 check_hgt() 中删除 if 语句时,是什么使 valid_passports 在数据正确时增加了它的值,但同时它导致我得到 KeyError(当我们查看 check_hgt() 的工作原理时,这很明显[unit = line[len(line)-2] + line[len(line)-1]这就是为什么上面提到的 if 语句是必要的 - 当没有测量单位时,它会自动显示不正确的值]),如果不能解决缺少检查的问题(列表粘在一起显示),则删除它。

请帮忙,谢谢。

标签: python

解决方案


推荐阅读