天天看點

python基礎-day16

day15回顧:
    異常: exception
        錯誤
        異常
        異常相關的語句:
        發出(觸發/抛出 )異常的通知:
            raise 語句
            assert 語句
        捕獲異常:
            try-except 語句
                try:
                    ...可以觸發異常的語句
                except 類型 as 變量名:
                    ...
                except:(通常放在所有except之後)
                    ...
                else:(沒有發生異常會執行)
                    ...
                finally:
                     ...
            做必須要做的事兒(執行必須要執行語句)
                try-finally語句
                    try:
                        ...
                    finally:
                        ...

    疊代器 iterator
        iter(可疊代對象)   傳回是的是疊代器
        next(疊代器)       從可疊代對象中取值
            在沒有資料時會觸發StopIteration異常
----------------------------------------------------

day16筆記:

生成器  Generator (python2.5及以後)
        生成器是能夠動态提供資料的可疊代對象
        生成器是程式運作時生成資料,與容器類不同,它通常不會在記憶體中儲存大量的資料,而是現用現生成
    生成器有兩種:
        1.生成器函數
        2.生成器表達式

    生成器函數:
        含有yield語句的函數是生成器函數,此函數被調用
        将傳回一個生成器對象
        yield翻譯為(産生或生成)
    yield 語句
        文法:
            yield 用于def 函數中,目的是将此函數作用生成器函數使用
            yield 用來生成資料,供疊代器的next(it)函數取值
    示例:
        gen_function.py 

            def myyield():
                '''這是一個生成器函數,此函數用來動态生成2,3,5,7'''
                yield 2
                yield 3
                yield 5
                yield 7
            gen=myyield()

            print(gen)  #gen是一個生成器對象
            it=iter(gen) #拿到疊代器
            print(next(it)) #2
            print(next(it)) #3
            print(next(it)) #5
            print(next(it)) #7
            #print(next(it)) #StopIteration
            print('---------以下用for語句---------')
            for x in myyield():
                print(x)
    生成器函數說明:
        1.生成器函數調用将傳回一個生成器對象,生成器對象是可疊代對象
        2.在生成器函數内調用return語句會觸發一個StopIteration異常(即生成資料結束)
    生成器函數的應用示例見:
        myinterger.py
        此示例示意用生成器函數建立生成從0開始到n結束的一系列整數(不包含n)
        def myinteger(n):
            i=0
            while i<n:
                yield i
                i+=1
        for x in myinteger(5):
            print(x)       

練習:
    寫一個生成器函數myeven(start,stop)用來生成從start開始到stop結束區間内的一系列偶數
    (不包含stop)
        def myeven(start,stop):
            for x in range(start,stop):
                if x%2==0:
                    yield x
        evens=list(myeven(10,20))
        print(evens)
        for x in myeven(5,10):
            print(x)
        L=[x for x in
         myeven(0,10)]
        print(L)    

生成器表達式:
    文法:
        (表達式 for 變量 in 可疊代對象 [if 真值表達式])
    說明:
        if 子句可以省略
    作用:
        用推導式形式建立一個新的生成器
    示例:
        gen=(x**2 for x in rnage(1,5))
        it=iter(gen)
        next(it)   #1
        next(it)   #4
        next(it)   #9
        next(it)   #16
        next(it)   #StopIteration


練習:
    已知有清單:
        l=[2,3,5.7.10,15]
    1)寫一個生成器函數,讓此函數能動态提供資料,資料為原清單的數字的平方+1
    2)寫一個生成器表達式,讓次表達式能動态提供資料,資料依舊為原清單數字的平方+1
    3)寫一個清單,此清單内的資料為原清單的數字的平方+1
        #1)
        l=[2,3,5,7,10,15]
        def myfunc(lst):
            for x in lst:
                b=x**2+1
                yield b
        #l=list(myfunc(l))
        print(l)
        #2)
        l2=list((x**2+1 for x in l))
        print(l2)

看下列程式的輸出結果有什麼不同:
l=[2,3,5,7]
l2=[x*10 for x in l]
it=iter(l2)
print(next(it))    #20
l[1]=30
print(next(it))    #30
-----------------
l=[2,3,5,7]
l2=(x*10 for x in l)
it=iter(l2)
print(next(it))    #20
l[1]=30
print(next(it))    #300
-----------------

練習:
    1.寫一個生成器函數,給出開始值begin,和終止值end,此生成器函數生成begin~end範圍内的全部素數(不含end)
    如:
        def is_prime(x):
            if x <2:
                return  False
            for j in range(2,x):
                if x%j==0:
                    return True
        def prime(begin,end):
            for x in range(begin,end):
                if is_prime(x):
                    yield x
        prime(begin,end)               
        l=list(primes(10,20))
        priint(l)   #[11,13,17,19]

疊代工具函數
    作用生成一個個性化的可疊代對象
函數 說明
    zip(iter1[,iter2,...])傳回一個zip生成器對象,此對象用于生成元組,元組的資料分别來自于參數中的每個可疊代對象
    生成元組的個數由最小的可疊代對象大小決定

    enumerate(iterable,start=0)傳回一個enumerate生成器對象,此對象生成類型為(索引,值)的元組,預設索引為零開始
    也可以用start指定
    示例:
            numbers=[10086,10000,10010,95588]
            names=['中國移動','中國電信','中國聯通']
            zip(numbers,names)
            for x in zip(numbers,names,range(1000)):
                print(x)
            for m,n in zip(names,numbers):
                print(m,'的客服電話是:',n)
            d=dict(zip(numbers,names))
            print(d)

    zip函數的實作(用生成器函數實作)
    示例見:
    myzip.py
#用生成器函數實作zip函數
            numbers=[10086,10000,10010,95588]
            names=['中國移動','中國電信','中國聯通']
            def myzip(iter1,iter2):
                it1=iter(iter1)
                it2=iter(iter2)
                while True:
                    try:
                        a=next(it1)
                        b=next(it2)    
                        yield(a,b)
                    except StopIteration:
                        return

            for x in myzip(numbers,names):
                print(x)
            d=dict(myzip(numbers,names))
            print(d)    

    enumerate 示例:
            names=['中國移動','中國電信','中國聯通']
            for t in enumerate(names,100):
                print(t) 
         -------------------------------------       
                (100, '中國移動')
                (101, '中國電信')
                (102, '中國聯通')

練習:
    寫一個程式,輸入任一行的文字,當輸入空時結束
    列印帶有行号的輸入結果
    def myword():
        l=[]
        while True:
            word=input('請輸入文字')
            if word=='':
                break        
            l.append(word)
    myword()
    def print_hang():
        for x in enumerate(l,1):
            print('第%d行',%l[0]'x)

位元組串和位元組數組:
    回顧:
        序列:
            清單,元組,字元串,位元組串,位元組數組
位元組串(也叫位元組序列): bytes
    作用:
        存儲以位元組為機關的資料
    說明:
        位元組串是不可改變的序列
    位元組:
        位元組是~255之間的整數,用來表示位元組的取值
    位 bit:
        1byte=8bit

建立空位元組串的字面值:
    b''
    b""
    b''''''
    b""""""
建立非空的位元組串字面值
    b'ABCD'
    b"ABCD"
    b'\x41\x42\x43\x44

位元組串構造函數bytes
bytes() 生成一個空的位元組串,等同于b''
bytes(整型可疊代對象) #用可疊代對象初始化一個位元組串
bytes(字元串,encoding='utf-8')用字元串的轉換編碼生成一個位元組串
示例:
    b=bytes() #b=b''
    b=bytes(range(65,70))   #b=b'ABCDE'
    b=bytes(5) #b=b'\x00\x00\x00\x00\x00'
    b=bytes('ABC','utf-8') #b=b'ABC'
    b=bytes("中文",'utf-8') 

bytes的運算:
    同其他的序列的運算規則相同
    + +=  *  *=  
    < <= >  >=  ==  !=
    in /not in 
    索引和切片

能用于位元組串和位元組數組的函數:
    len(x),max(x),min(x)
    sum(x),any(x),all(x)

bytes和str的差別
    bytes 存儲位元組(位元組是0~255的整數)
    str    存儲unicode字元(字元是0~65536的整數)

bytes與str轉換
        編碼(encode)
    str----------> bytes
        b=s.encode(encoding='utf-8')
                
          解碼(decode)
    bytes-------------> str
        s=b.decode(encoding='utf-8')
    示例:
        s='我是中文'
        b=s.encode('utf-8')
        s2=b.decode('utf-8')

位元組數組 bytearray
    可變的位元組序列

位元組數組構造的函數(bytearray)
    位元組串構造函數bytes
    bytearray() 生成一個空的位元組數組,等同于b''
    bytearray(整型可疊代對象) #用可疊代對象初始化一個位元組數組
    bytearray(字元串,encoding='utf-8')用字元串的轉換編碼生成一個位元組數組

運算規則:
    運算規則同序列完全相同
        + +=  *  *=  
    < <= >  >=  ==  !=
    in /not in 
    索引和切片
    位元組數組支援索引和切片的指派操作,規則同清單的指派規則完全相同
位元組串的方法見文檔:
    python_base_doc_html/bytearray.html

練習:
    有一個bytearray位元組數組:
    ba=bytearray(b'a1b2c3d4')
    如何得到位元組串'1234'和'abcd'
   将上述位元組數組改為:
    ba=bytearray(b'A1B2C3D4')

    ba=bytearray(b'a1b2c3d4')
    b1=ba[1::2]
    b1=bytes(b1)    #b'1234
    b2=ba[::2]      #b'abcd
    b2=bytes(b2) 
    ba[::2]=range(65,69)  #b'A1B2C3D4

練習:
  1.列印9x9乘法表
    1x1=1
    1x2=2 2x2=4
    1x3=3 2x3=6
    ...
    1x9=9.............9x9=81

   2.寫一個生成器函數,myxrange(start,stop,syep)來生成一系列整數
        要求功能與range完全相同
        不允許調用range函數和清單
        然後用自己寫的myxrnage函數
        求1~100内奇數的平方和
   3.寫一個myfilter生成器函數,功能與filter函數完全相同
    如:
    def myfilter(fn,iter1)
    ...
    L=[x for x in myfilter(lambda x:x%2,range(10))]
    #L=[1,3,5,7,9]
   4.将以前所所有練習自己看不懂的代碼重寫一遍























































           
下一篇: 2020-04-07