01-python拾遺

來源:互聯網
上載者:User

標籤:刪除   ted   資料   位置   lamda   插入   remove   pytho   python   

列表複習
append(x)追交到鏈尾
extend(L)追加一個列表 等價於 +=
insert(i,x)在位置i處插入x
remove(x) 刪除一個值為x的元素 如果沒有拋出異常
sort() 直接修改列表為排序過的
sorted(L) 返回排序後的L

元祖 一旦初始化便不能修改的資料結構 比列錶快
集合 (set) 無序不重複的元素集 不保證是有序
字典(dict) 關鍵字不可變類型 如字串 整數 只包含 不可變對象的元祖

關於字典 功能非常強大 我們可以做的事情很多比如

def leijia(x,y):    a = 0    for i in xrange(x,y+1):        a += i    return adef plus(x,y):    return  x+ymatruix = {    ‘+‘:plus,    ‘leijia‘:leijia,}def size(x,oper,y):    return matruix[oper](x,y)print(size(1,‘leijia‘,2))

我們可以做一個switch case的模式

 

#filter

a = [1,2,3,4]

filter(lamda x:x%2,a)

[1,3]

#map 返回序列 為對原序列每個元素分別調用function獲得的可以傳入多個序列 但是function 也要有相應多的參數

 map(lambda x,y,z:x+y+z,range(1,3),range(3,5),range(5,7))

計算過程  1+3+5 = 9 2+4+6=12 結果 [9,12]

接下來我們要實現

l1 = [1,2,3,4]
oper = [‘+‘,‘-‘,‘leijia‘,‘*‘]
l2 = [2,44,55,66]

#l1[0] "oper[0]" l2[0]=? ...



 

def plus(x,y):    return  x+ydef jian(x,y):    return x-ydef leijia(x,y):    a = 0    for i in(x,y):        a +=1    return  adef chengji(x,y):    return x*ymy_dict = {    ‘+‘:plus,    ‘-‘:jian,    ‘leijia‘:leijia,    ‘*‘:chengji,}l1 = [1,2,3,4]oper = [‘+‘,‘-‘,‘leijia‘,‘*‘]l2 = [2,44,55,66]#L1[0] "oper[0]" L2 = ? ....res = map(lambda x,y,z:my_dict[y](x,z),l1,oper,l2)print(list(re

 

01-python拾遺

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.