COMP9021 Principles of Programming WEEK4 Optional

sort函數(shù),lambda函數(shù),filter函數(shù),for_else循環(huán)控制

1. sort()

L.sort(key)對L使用key規(guī)則進(jìn)行排序,返回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的默認(rèn)是從小到大排序

2.sorted()

sorted(L)對L進(jìn)行排序,返回排序后的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 函數(shù)輸入值: 函數(shù)body。

f = lambda : 3
f()
>>>
3
#含義是定義一個無輸入值的函數(shù),返回數(shù)字3
f = lambda x: 3
print(f(4), f(2))
>>>
3  3
#含義是定義一個輸入值為x的函數(shù),無論x是多少,返回值都是3

4.order排序

對于order,默認(rèn)排序規(guī)則是先比較第一個數(shù)字,再比較第二個數(shù)字。

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.制定復(fù)雜排序規(guī)則

(1)以order兩數(shù)之和的大小進(jìn)行排序

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函數(shù)簡化程序

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

(3)以絕對值大小進(jìn)行排序

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)偶數(shù)在前奇數(shù)在后

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)偶數(shù)在前奇數(shù)在后,且按大小順序輸出

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方法多次限定實(shí)現(xiàn)

6. filter()

filter(規(guī)則,object)根據(jù)規(guī)則篩選符合條件的object。
(1)篩選質(zhì)數(shù)

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)篩選偶數(shù)

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循環(huán)全部完成,則執(zhí)行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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • http://python.jobbole.com/85231/ 關(guān)于專業(yè)技能寫完項(xiàng)目接著寫寫一名3年工作經(jīng)驗(yàn)的J...
    燕京博士閱讀 7,792評論 1 118
  • 個人筆記,方便自己查閱使用 Py.LangSpec.Contents Refs Built-in Closure ...
    freenik閱讀 67,945評論 0 5
  • 第一章 為什么要關(guān)心Java 8 使用Stream庫來選擇最佳低級執(zhí)行機(jī)制可以避免使用Synchronized(同...
    謝隨安閱讀 1,560評論 0 4
  • 因?yàn)楣纠镒錾坛情_發(fā)用的是OpenCart,上級讓我先熟悉這個系統(tǒng),借此機(jī)會詳細(xì)解讀一下商品管理這一功能(也是為了...
    山有木兮_卿有意閱讀 1,804評論 0 2
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,899評論 0 33

友情鏈接更多精彩內(nèi)容