2019.9.25作业

# 前戏
info_lt = []
att_lt = ['name', 'sex', 'age', 'salary']
with open('info.txt', 'r', encoding='utf8') as fr:
    for info in fr:
        info = info.strip().split(' ')
        # eve = {k:v for k in att_lt for v in info }
        eve = dict(zip(att_lt, info))
        info_lt.append(eve)
    print(info_lt)


# 1薪资最高
def max_salary():
    sa_lt = []
    for i in range(len(info_lt)):
        sal = info_lt[i]['salary']
        sa_lt.append(sal)
    max_sal = max(sa_lt)
    print(max_sal)


max_salary()


# 2年纪最低
def min_age():
    age_lt = []
    for i in range(len(info_lt)):
        ag = info_lt[i]['age']
        age_lt.append(ag)
    min_ag = min(age_lt)
    print(min_ag)


min_age()


# 3名字首字母大写
def cap():
    nam_lt = []
    for i in range(len(info_lt)):
        nam = info_lt[i]['name']
        nam_lt.append(nam)
    name_lt = list(map(lambda i: i.capitalize(), nam_lt))
    print(name_lt)


cap()


# 4 过滤a开头
def beginwitha():
    nam_lt = []
    for i in range(len(info_lt)):
        nam = info_lt[i]['name']
        nam_lt.append(nam)
    name_lt = list(filter(lambda i: not i.startswith('a'), nam_lt))
    print(name_lt)


beginwitha()


# 5 斐波那契
def fib(x):
    if x <= 2:
        return 1

    return fib(x - 2) + fib(x - 1)


for i in range(1, 5):
    print(fib(i))

# 6 递归取值
lt = [1, 2, [3, [4, 5, 6, [7, 8, [9, 10, [11, 12, 13, [14, 15]]]]]]]


def get_lst(lt):
    if not type(lt[len(lt) - 1]) == list:
        for i in range(len(lt)):
            print(lt[i])
        return
    for i in range(len(lt)):
        if not i == len(lt) - 1:
            print(lt[i])
        if i == (len(lt) - 1):
            lt = lt[-1]
            return get_lst(lt)


get_lst(lt)

posted @ 2019-09-25 21:31  Agsol  阅读(99)  评论(0编辑  收藏  举报