*的使用技巧

# *的使用技巧
ages = [1, 2, 4, 8]


def drop_first_last(ages):
    f, *m, last = ages
    return sum(m) / len(m)


print(drop_first_last(ages))

# ############

records = [
    ['a', 1, 2],
    ['b', 'hello'],
    ['a', 'b', 'c']
]


def do_a(x, y):
    print('a', x, y)


def do_b(s):
    print('b', s)


for i, *j in records:
    if 'a' == i:
        do_a(*j)
    elif 'b' == i:
        do_b(*i)

records2 = ('a', '50', 111, (1, 23, 2020))

name, *_, (*_, year) = records2
print(name)
print(year)

posted @ 2020-07-27 14:26  fly_bk  阅读(115)  评论(0编辑  收藏  举报