COMP9021 Principles of Programming WEEK4 Optional

sort函数,lambda函数,filter函数,for_else循环控制

1. sort()

L.sort(key)对L使用key规则进行排序,返回None值,使用时不要assign一个list = .sort()

from random import randrange
L = [randrange(20) for _ in range(10)]
L.sort()
L
>>>
[0, 2, 5, 6, 7, 7, 16, 18, 19, 19]
#key的默认是从小到大排序

2.sorted()

sorted(L)对L进行排序,返回排序后的list

from random import randrange
L = [randrange(20) for _ in range(10)]
L = sorted(L)
L
>>>
[6, 7, 7, 11, 11, 11, 13, 13, 13, 17]

3.lambda

lambda简化了def function的书写方法,lambda 函数输入值: 函数body。

f = lambda : 3
f()
>>>
3
#含义是定义一个无输入值的函数,返回数字3
f = lambda x: 3
print(f(4), f(2))
>>>
3  3
#含义是定义一个输入值为x的函数,无论x是多少,返回值都是3

4.order排序

对于order,默认排序规则是先比较第一个数字,再比较第二个数字。

L = [(randrange(20), randrange(20)) for _ in range(10)]
L.sort()
print(list(L))
>>>
[(2, 11), (2, 14), (3, 6), (6, 10), (7, 8), (10, 3), (12, 14), (13, 7), (14, 9), (17, 10)]

5.制定复杂排序规则

(1)以order两数之和的大小进行排序

L = [(randrange(20), randrange(20)) for _ in range(10)]
def f(x):
    return x[0] + x[1]
L.sort(key = f)
print(list(L))
>>>
[(9, 0), (5, 8), (12, 3), (16, 7), (14, 12), (15, 12), (18, 12), (17, 14), (14, 19), (18, 16)]

(2)借助lambda函数简化程序

L = [(randrange(20), randrange(20)) for _ in range(10)]
L.sort(key = lambda x: x[0] + x[1])
print(list(L))

(3)以绝对值大小进行排序

L = [randrange(-19, 20) for _ in range(10)]
L.sort(key = abs)
print(list(L))
>>>
[1, 1, 2, 4, -4, -5, 5, -6, 9, 13]

(4)偶数在前奇数在后

L = [randrange(20) for _ in range(20)]
L.sort(key = lambda x: x % 2)
print(list(L))
>>>
[16, 2, 2, 4, 14, 12, 0, 14, 10, 6, 9, 9, 17, 11, 15, 17, 15, 11, 15, 7]

(5)偶数在前奇数在后,且按大小顺序输出

L = [randrange(20) for _ in range(20)]
L.sort(key = lambda x: (x % 2, x))
print(list(L))
>>>
[0, 0, 0, 0, 2, 4, 12, 14, 16, 18, 18, 3, 3, 7, 11, 13, 13, 15, 15, 17]
如果排序有多种要求,可以借助上文提到的order方法多次限定实现

6. filter()

filter(规则,object)根据规则筛选符合条件的object。
(1)筛选质数

from math import sqrt
L = [randrange(100) for _ in range(30)]
def is_prime(x):
    if x < 2:
        return False
    for d in range(2, round(sqrt(x)) + 1):
        if x % d == 0:
            return False
    return True
L_filter = list(filter(is_prime, L))
L_filter.sort()
L_filter
>>>
[13, 17, 79, 89, 97]

(2)筛选偶数

L = [randrange(100) for _ in range(30)]
set(filter(lambda x: not x % 2, L))
>>>
{6, 12, 24, 34, 42, 50, 64, 68, 70, 78, 80, 82, 98}

7. for else

如果for循环全部完成,则执行else后语句,否则跳过

from random import randrange

for _ in range(7):
    generated_number = randrange(-10, 60)
    print(f'Generated number: {generated_number}')
    if generated_number < 0:
        continue
    if generated_number >= 50:
        break
    print('\tUnless it is a single digit, will determine what it is equal to modulo 4')
    if generated_number < 10:
        pass
    elif generated_number % 4 == 1:
        print('\tEqual to one modulo four')
    elif generated_number % 4 == 2:
        print('\tEqual to two modulo four')
    elif generated_number % 4 == 3:
        print('\tEqual to three modulo four')
    else:
        print('\tDivisible by four')
else:
    print('No number greater than 49 has been generated')
>>>
Generated number: 22
    Unless it is a single digit, will determine what it is equal to modulo 4
    Equal to two modulo four
Generated number: 46
    Unless it is a single digit, will determine what it is equal to modulo 4
    Equal to two modulo four
Generated number: 40
    Unless it is a single digit, will determine what it is equal to modulo 4
    Divisible by four
Generated number: 50
>>>
Generated number: 27
    Unless it is a single digit, will determine what it is equal to modulo 4
    Equal to three modulo four
Generated number: 35
    Unless it is a single digit, will determine what it is equal to modulo 4
    Equal to three modulo four
Generated number: 4
    Unless it is a single digit, will determine what it is equal to modulo 4
Generated number: -4
Generated number: 2
    Unless it is a single digit, will determine what it is equal to modulo 4
Generated number: 3
    Unless it is a single digit, will determine what it is equal to modulo 4
Generated number: 29
    Unless it is a single digit, will determine what it is equal to modulo 4
    Equal to one modulo four
No number greater than 49 has been generated

你可能感兴趣的:(COMP9021 Principles of Programming WEEK4 Optional)