体育收入排行2012-2019(用列表)

def read_file():
    with open('2012-19sport.csv', 'r', encoding='utf-8') as data:
        # ls1 = data.readline().strip().split(',')
        data.readline()
        ls2 = []
        for line in data.readlines():
            ls2.append(line.strip().split(','))
        # print(ls1)
        # print(ls2)
        for i in range(len(ls2)):
            ls2[i][0] = ls2[i][0].replace('#', '')
        # print(ls2)
        # print(ls1 + ls2)
        return ls2


def fun1(year, ls):
    k = int(input())
    ls_one_year = []
    for element in ls:
        # print(element)
        if element[6] == str(year):
            ls_one_year.append(element)
    for element in ls_one_year[:k]:
        print(*element, sep=' | ')


def fun2(ls):
    year = int(input())
    ls_one_year = []
    for item in ls:
        if item[6] == str(year):
            ls_one_year.append(item)
    ls1 = sorted(list(set(item[5] for item in ls_one_year)))
    for element in ls1:
        print(f'{ls1.index(element) + 1}: {element}')

    ls_key = [i for i in range(1, len(ls1) + 1)]
    dt1 = dict(zip(ls_key, ls1))
    key = int(input())
    # print(dt1)
    ls_sport = []
    for element in ls_one_year:
        if element[5] == dt1[key]:
            ls_sport.append(element)
    # print(ls_sport)
    for element in ls_sport:
        print(*element, sep=' | ')

    ls_pay = []
    for element in ls_sport:
        ls_pay.append(float(element[2].replace('$', '').replace(' M', '')))
    # print(ls_pay)
    print('TOTAL: ${:.2f} M'.format(sum(ls_pay)))


if __name__ == '__main__':
    ls_return = read_file()
    option = input()
    try:
        option = int(option)
        if 2012 <= option <= 2019:
            fun1(option, ls_return)
        else:
            print('Wrong Input')
    except Exception as e:
        option = option.lower()
        if option == 'sport':
            fun2(ls_return)
        else:
            print('Wrong Input')

上一篇:ciscn_2019_n_7(exit_hook)


下一篇:蓝桥杯c组c/c++ 字符串2019