#Encoding=utf-8__author__='[email protected]'################# Python comes with a sort interface ######################python with very powerful sort functions, sorted, or sort#Sorted is the built-in function, sort is the function of the list class, they all use the same, the following sorted as an example#using Python's sorted function for sorting, this function can enter three parameters, the key parameter is used to declare according to which part of the data is sorted, reversed parameter#To declare a positive or reverse order, CMP can define rules for sortingdefSORT_SORTED_V1 (sort_list):" "using the underscore of each entry in data, the last item is reversed @data The list needs to sort the data this function if input data=[' ljxz_sa_100 ', ' ljxz_sa_78 ', ' ljxz_sa_910 ', ' LJX Z_sa_90 '] will return [' ljxz_sa_910 ', ' ljxz_sa_100 ', ' ljxz_sa_90 ', ' ljxz_sa_78 ']" " returnSorted (Sort_list, key=LambdaX:int (X.split ('_') [-1]), reverse=True)defSort_sorted_v2 (sort_list):" "by defining the CMP parameters, the custom sorting rules are sorted according to the length of the data @data list needs to sort the data this function if input data=[1, 8, ' Fefe ', 678, 35, 4] will return [1, 8, 4, 3 5, 678, ' Fefe ']" " defmy_cmp (x, y): F=LambdaX:len (str (x))returnCMP (f (x), F (y))returnSorted (Sort_list, cmp=Lambdax, y:my_cmp (x, y))################# General sorting method ######################1. Insert Sortdefinsertion_sorting (sort_list): Sort_list_len=Len (sort_list)ifSort_list_len > 1: #to traverse a list from the second number of the list forIinchRange (1, Sort_list_len): J= I-1Tmp_item=Sort_list[i]#from the position of I, to the left to traverse the list, once the number is greater than I for the number, let this large number move backwards whileJ >= 0 andSORT_LIST[J] >tmp_item:sort_list[j+ 1] =Sort_list[j] J-= 1#If there are numbers moving, put the number I corresponds to in the position of the j+1 ifJ! = I-1: Sort_list[j+ 1] =Tmp_itemreturnsort_list#2. Bubble sortdefbubble_sorting (sort_list):" "by comparing the size of two adjacent numbers, move large to the right, move the largest number of left I to the right, and then move the largest number of left i-1 to the far right, and so on. I start with the length of the list" "Sort_list_len=Len (sort_list)ifSort_list_len > 1: forIinchRange (sort_list_len-1): forJinchRange (Sort_list_len-1-i):ifSORT_LIST[J] > sort_list[j + 1]: sort_list[j], sort_list[j+ 1] = sort_list[j + 1], Sort_list[j]returnsort_list#2. Bubble sortdefbubble_sorting1 (sort_list):" "by comparing the size of two adjacent numbers, move large to the right, move the largest number of left I to the right, and then move the largest number of left i-1 to the far right, and so on. I start with the length of the list" "Sort_list_len=Len (sort_list)ifSort_list_len > 1: I=Sort_list_len whileI > 1: forJinchRange (i-1): ifSORT_LIST[J] > sort_list[j + 1]: sort_list[j], sort_list[j+ 1] = sort_list[j + 1], sort_list[j] I-= 1returnsort_list#3. Select Sortdefselection_sorting (sort_list):" " " "Sort_list_len=Len (sort_list)ifSort_list_len > 1: forIinchRange (sort_list_len-1): Min_=I#find the minimum value of all the numbers after I (including i), with I exchange forJinchRange (I, Sort_list_len):ifSort_list[min_] >Sort_list[j]: Min_=JifMin_! =I:sort_list[min_], Sort_list[i]=Sort_list[i], Sort_list[min_]returnsort_list#Quick Sortdefquick_sorting (sort_list):" "1. Input i and j,k equal to the value of subscript I, I is the beginning subscript of the list, J for the end subscript 2.j in turn minus one, until there is sort_list[j]<k, make sort_list[i] = Sort_list[j] 3.i in turn, until there is an SOR T_list[i]>k Sort_list[j] = sort_list[i] 4. Repeat 2, 3, until I and j are equal, so that the position of the k is lined up, because the left side is smaller than it, and the right side is 5 larger than it. Consider the number of the left of K and the number on the right as two new lists, performing 1,2,3,4,5 steps for each of them, until the new list has a length equal to 1" " def_quick_sorting (i, j):if notJ >I:returnI_, J_=I, J K=Sort_list[i] whileI! =J: whileI! =J:ifSORT_LIST[J] <K:sort_list[i]=Sort_list[j] BreakJ-= 1 whileI! =J:ifSort_list[i] >K:sort_list[j]=Sort_list[i] BreakI+ = 1Sort_list[i]=k _quick_sorting (i_, I-1) _quick_sorting (i+ 1, J_) _quick_sorting (0, Len (sort_list)-1) returnsort_listif __name__=='__main__': Sort_list= ['ljx_sa_100','ljx_sa_78','ljx_sa_910','ljx_sa_90'] Printsort_sorted_v1 (sort_list) sort_list= [1, 8,'Fefe', 678, 35, 4] Printsort_sorted_v2 (sort_list) sort_list= [1, 8, 48, 678, 35, 4] Printinsertion_sorting (sort_list) sort_list= [1, 8, 48, 678, 35, 4] Printbubble_sorting (sort_list) sort_list= [1, 8, 48, 678, 35, 4] Printselection_sorting (sort_list) sort_list= [1, 8, 48, 678, 35, 4] PrintQuick_sorting (Sort_list)
#Encoding=utf-8__author__='Administrator'############ #python内建查找函数 ###################1.index This is the Python list class comes with a lookup functiondefsearch_index (SRC, target):" "@param List of src:list lookups @param target: Destination @return found: Find subscript for Target" " Try: returnSrc.index (target)exceptValueError:return-1#2.find function to find a stringdefsearch_find (SRC, target):" "@param the string found by the Src:str @param target: Destination @return: Find the target's subscript, if the query is not, return-1" "Src.find (target)#3.re python inside the regular module, the function is very powerful, the actual development of the more use of this methoddefsearch_re (SRC, patt):" "@param src:str Lookup string @param target: Regular expression @return: List of all results that match the regular" " ImportRereturnre.findall (Patt, SRC)############### #一般的查询算法 #####################1. Sequential Queriesdefsearch_sequence (SRC, target):" "@param src:list Lookup source list @param target: Find the target @return: Find the target's subscript, if the query is not, return-1" " forIndex, valueinchEnumerate (SRC):iftarget = =Value:returnIndexreturn-1#2. Two-way querydefsearch_dichotomy (SRC, target):" "@param src:list The list of sources to find, which needs to be sorted and ascending @param target: Find the target @return: Find the target's subscript, and return 1 if the query is not reached ." " Low=0 hight=len (SRC) whileHight >Low:mid= (low + hight)/2ifSrc[mid] = =Target:returnTargetelifSrc[mid] <Target:low=MidElse: Hight=Midreturn-1if __name__=='__main__': src= [1, 2, 3, 7, 9, 54, 666] PrintSearch_index (SRC, 554) src='123486875' PrintSearch_index (SRC,' -') src='123486875' PrintSearch_re (SRC,' -') src= [1, 2, 3, 7, 9, 54, 666] PrintSearch_sequence (SRC, 9) PrintSearch_dichotomy (SRC, 0)
Python Sorting and querying