1, lambda functions
- lambda can only handle simple function
1 # a simple function 2 DEF FUNC (X, Y): . 3 return X + Y . 4 # implemented by the lambda . 5 the lambda X, Y: X + Y
2, sorted ranking function
-
function sorted
sorted function is ordered, is a sort iterable type of data, the general type of data to be iterable method of this sort, the custom function can be sorted ordering rules#sorted函数的类型 sorted(iterable,key=none,reverse=False) lst = [{"id":1,"name":"shuai","age":25},{"id":3,"name":"xiang","age":18},{"id":2,"name":"aoli","age":27}] # Age Sort L1 = sorted (LST, Key = the lambda dic: dic [ " Age " ]) Print (L1) # by name alphabetical order L2 = sorted (LST, Key = the lambda dic: dic [ " name " ] [0]) Print (L2)
3, filter function
- filter function
Which is determined by the function func iterable elements Meets
#filter(func,iterable) lst = [19,20,18,30,25,17] def func(age): return age >18 f = filter(func,lst) print(f) print("__iter__" in dir(f)) for i in f: print(i)
4, map function
- map mapping function
#map(func,iterable) lst = [1,5,3,5,4,8,7,9] l1 = map(lambda x : x**2,lst) print(list(l1))
5, recursive
The main feature is the recursive calls itself, the end must be specified, the deepest in the official said the number of recursive Python 1000
A recursive infinite loop
= 1 ceng DEF Fun (ceng): Print (ceng) Fun (ceng +1 ) Fun (ceng) operating results 995 996 997 998Traceback (MOST recent Results Last Call): File " E: / python_iteam / LivePython / Day6 / directory / recursive .py " , Line 26, in <Module> Fun (ceng) File " E: / python_iteam / LivePython / Day6 / directory / recursive .py " , Line 25, in Fun Fun (ceng +1 ) File " E: / python_iteam / LivePython / day6 / directory / recursive .py " , Line 25, in fun fun(ceng+1) File "E:/python_iteam/LivePython/day6/目录/递归.py", line 25, in fun fun(ceng+1) [Previous line repeated 994 more times] File "E:/python_iteam/LivePython/day6/目录/递归.py", line 24, in fun print(ceng) RecursionError: maximum recursion depth exceeded while calling a Python object
By recursively through all the files in a folder
Import os DEF FUNC (file_dir, ceng): list = the os.listdir (file_dir) # by os module to obtain directory below a directory to list indicates for File in list: # circulation list, to distinguish between files and directories file_path = os.path .join (file_dir, File) # get the absolute path IF os.path.isdir (file_path): Print ( " \ t " * ceng + " % S: " % File) FUNC (file_path, ceng +1) # recursive function the else : Print ( " \ t "*ceng+"%s"%file) func("F:\\",0)
6, a binary search
- The first, direct circulation
. 1 List = [1,3,4,5,6,8,9,10,24,34,45,54,63 ] 2 Lift = 0 . 3 right = len (List) . 4 n-int = (INPUT ( " Please enter a number: " )) . 5 the while Lift < right: . 6 MID = (+ Lift right) // 2 . 7 IF List [MID]> n-: . 8 right = MID . 9 elif List [MID] < n-: 10 Lift MID = . 1 + . 11 the else : 12 is Print ( " found a few% s, subscripts S% " %(List [MID], MID)) 13 is BREAK 14 the else : 15 Print ( " List in this number is not " )
- The second, by a recursive function to create a new list
1 list = [1,3,4,5,6,8,9,10,24,34,45,54,63] 2 lift = 0 3 right = len(list) 4 n = int(input("请输入一个数:")) 5 def func(list,n): 6 lift = 0 7 right = len(list) 8 mid = (lift + right)//2 9 if mid > 0: 10 if list[mid] > n: 11 new_list = list[:mid] 12 func(new_list,n) 13 elifList [MID] < n-: 14 new_list The List = [MID +. 1 :] 15 FUNC (new_list The, n-) 16 elif List [MID] == n-: . 17 Print ( " found a number S% " % List [MID]) 18 the else : 19 Print ( " not found " ) 20 FUNC (List, the n-)
- Third, the recursive function is given about
1 list = [1,3,4,5,6,8,9,10,24,34,45,54,63] 2 lift = 0 3 right = len(list) 4 n = int(input("请输入一个数:")) 5 6 def func(list,lift,right,n): 7 mid = (lift+right)//2 8 if lift < right: 9 if list[mid] > n: 10 right = mid 11 func(list, lift, right, n) 12 elif list[mid] < n : 13 lift = mid+1 14 FUNC (List, Lift, right, n-) 15 elif List [MID] == n-: 16 Print ( " found% s, position S% " % (List [MID], MID)) . 17 the else : 18 is Print ( " not found " ) 19 FUNC (List, Lift, right, the n-)