Java教程

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

本文主要是介绍体育收入排行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')

这篇关于体育收入排行2012-2019(用列表)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!