編程的方法論編程
面向過程:找到問題的 數據結構
函數式:不可變、不用變量保存狀態、不修改變量app
面向對象: 函數
高階函數:編碼
知足倆個特性任意一個即爲高階函數spa
1.函數的傳入參數是一個函數名code
2.函數的返回值是一個函數名對象
append() 方法用於在列表末尾添加新的對象。blog
map函數:ip
num_l=[1,2,10,5,3,7]# 計算該列表中數的平方值 方法一: # ret=[] # for i in num_l: # ret.append(i**2) # print(ret) 方法二: def map_test(array): ret=[] for i in num_l: ret.append(i**2) #
return ret ret=map_test(num_l) print(ret)
方法三:
num_l=[1,2,10,5,3,7]
#lambda x:x+1
def add_one(x):
return x+1
#lambda x:x-1
def reduce_one(x):
return x-1
#lambda x:x**2
def square_one(x):
return x**2
def map_test(func,array):
ret=[]
for i in num_l:
res=func(i)
ret.append(res)
return ret
print(map_test(add_one,num_l))
print(map_test(reduce_one,num_l))
#print(map_test(lambda x:x**2,num_l))
print(map_test(square_one,num_l))
#終極版本
num_l=[1,2,10,5,3,7]
def map_test(func,array):
ret=[]
for i in num_l:
res=func(i) #add_one
ret.append(res)
return ret
print(map_test(lambda x:x+1,num_l))
res=map(lambda x:x+1,num_l)
print('內置函數map,處理結果',res)
# for i in res:
# print(i)
print(list(res))
print('傳的是有名函數',list(map(reduce_one,num_l)))
#大寫轉換
msg='wuxiping'
print(list(map(lambda x:x.upper(),msg)))
filter函數:
# movie_people=['sb_alex','sb_wupeiqi','linhaifeng','sb_yuanhao'] # def filter_test(array): # ret=[] # for p in array: # if not p.startswith('sb'): # ret.append(p) # return ret # print(filter_test(movie_people)) # movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb'] # def sb_show(n): # return n.endswith('sb') # def filter_test(func,array): # ret=[] # for p in array: # if not func(p): # ret.append(p) # return ret # res=filter_test(sb_show,movie_people) # print(res) #終極版本 movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb'] # def sb_show(n): # return n.endswith('sb') #lambda n:n.endswith('sb') def filter_test(func,array): ret=[] for p in array: if not func(p): ret.append(p) return ret res=filter_test(lambda n:n.endswith('sb'),movie_people) print(res)
#filter函數
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
#print(filter(lambda n:n not n.endswith('sb',movie_people)))
res=filter(lambda n:not n.endswith('sb'),movie_people)
print(list(res))
reduce函數:
from functools import reduce # num_l=[1,2,3,100] # res=0 # for num in num_l: # res+=num # print(res) #相加 # num_l=[1,2,3,100] # def reduce_test(array): # res=0 # for num in num_l: # res+=num # return res # print(reduce_test(num_l)) #相乘 # num_l=[1,2,3,100] # def reduce_test(func,array): # res=array.pop(0) # for num in array: # res=func(res,num) # return res # print(reduce_test(lambda x,y:x*y,num_l)) # 指定初始值 # num_l=[1,2,3,100] # def reduce_test(func,array,init=None): # if init is None: # res=array.pop(0) # else: # res=init # for num in array: # res=func(res,num) # return res # print(reduce_test(lambda x,y:x*y,num_l,100)) #reduce函數 num_l=[1,2,3,100] print(reduce(lambda x,y:x+y,num_l,1)) print(reduce(lambda x,y:x+y,num_l,))
總結:
#總結: # map函數:處理序列中的每一個元素,獲得的結果是一個‘列表’,該‘列表’元素個數和位置與原來同樣 #filter函數:遍歷序列中的每一個元素,判斷每一個元素獲得布爾值,若是是True則留下來獲得的結果是一個‘列表’ # people=[{'name':'alex','age':10000}, # {'name': 'wupeiqi', 'age': 10000}, # {'name': 'yuanhao', 'age': 9000}, # {'name': 'linhaifeng', 'age': 18},] # print(list(filter(lambda p:p['age']<=18,people))) #reduce函數:處理 一個序列,把序列進行合併操做 # num_l=[1,2,3,100] # print(reduce(lambda x,y:x+y,num_l,1)) # print(reduce(lambda x,y:x+y,num_l,))
內置函數:
# print(abs(-1)) # print(all([1,2,'1']))#作布爾運算(全部的都是True才返回True) # name='你好' # print(bytes(name,encoding='utf-8')) #編碼,三個字節 # print(bytes(name,encoding='utf-8').decode('utf-8'))# 解碼 # print(bytes(name,encoding='gbk'))#兩個字節 # print(bytes(name,encoding='gbk').decode('gbk')) # #print(bytes(name,encoding='ascii'))#ascii 不能編碼中文 # print(chr(100)) # print(dir(all)) # print(divmod(10,3)) # dic={'name':'alex'} # dic_str=str(dic) # print(dic_str) # print(eval(dic_str))
eval()函數:以Python的方式解析並執行字符串,並將返回的結果輸出
# d1=eval(dic_str)#把字符串中的數據結構給提取出來 # print(d1['name']) # a='1+2*(3/3-1)-2'#把字符串中的表達式進行運算 # print(eval(a)) #可hash的數據類型即不可變數據類型,不可hash的數據類型便可變數據類型 # print(hash('dhfsaklefownfs2134254')) # print(hash('-03thjsdnfkgqopw3utjlsdfml;')) # name='alex' # print(hash(name)) # print(hash(name)) # print(hash(name)) # print(help(dir)) # print(bin(10)) #10進制轉換成2進制 # print(hex(12)) #16 # print(oct(12)) #8 # print(isinstance(1,int)) # print(isinstance('abc',int)) # print(isinstance('abc',str)) name='哈哈哈哈' print(globals()) print(__file__) print(locals())