python學習之總結,python總結

來源:互聯網
上載者:User

python學習之總結,python總結

迭代器:def gen():    a = 100    yield a    a = a * 8    yield a    yield 1000for i in gen():    print(i)建立一個函數,迴圈體,yield迴圈到此就返回一個值。調用函數,列印出迴圈結果:1008001000表推導:L = [x**2 for x in range(10)]print(L)等價於:M = []for x in range(10):    M.append(x**2)print(M)列印出結果:[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]xl = [1,3,5]yl = [9,12,13]L  = [ x**2 for (x,y) in zip(xl,yl) if y > 10]print(L)等價於for (x,y) in zip(xl,yl):    if y > 10:        print(x)我們可以先列印出zip對應的Y值大於10的X的值列印出X的值:>>3>>5列印出L的值:[9, 25]

  

#lambda函數def test(f,a,b):    print ('test')    print(f(a,b))test((lambda x,y:x**2+y),6,9)#使用lambda匿名函數給f參數傳遞值,可以使不同形式的。列印結果:>>test(f(a,b))相當於a=6傳遞給x,b=9傳遞給y,組合成f的值>>45#map()re = map((lambda x:x+3),[1,3,5,7])print(list(re))#map中的一個參數x,將後面列表中的值一次傳遞給x,相當於列表的值依次加3,在取值中以列表的形式。列印結果:[4, 6, 8, 10]re2 = map((lambda m,n:m**n),[1,2,3,4],[5,6,7,8])print(list(re2))列印結果:[1, 64, 2187, 65536]#filter()def abc(a):    if a > 100:        return True    else:        return Falsenewlist = filter(abc,[100,30,101,200])print(list(newlist))#建立一個函數abc中有一個參數a。newlist中函數filter將列表的值一次傳給函數abc中的參數a,取值用列表形式。列印結果:[101, 200]#reduce()#from functools import reduce  #因為python不支援reduce函數,可以在functools庫中匯入單個reduce函數import functools #可以直接匯入整個庫print(functools.reduce(lambda x,y:x+y,range(1,101)))#reduce函數是將列表中的值的第一次傳遞兩個參數相加的和3,在傳遞一個參數3相加。相當於1-100相加的和。列印結果:>>5050

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.