標籤:刪除 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拾遺