10 examples of 'python sort list by first element' in Python

Every line of 'python sort list by first element' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
22def Quick_Sort(list):
23 Quick(list, 0, len(list) - 1)
70def sorted(lst, cmp=None, key=None, reverse=None):
71 "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"
72 sorted_lst = list(lst)
73 sorted_lst.sort(cmp, key, reverse)
74 return sorted_lst
23def qsort(lst):
24 """Return a sorted copy of the input list.
25
26 Input:
27
28 lst : a list of elements which can be compared.
29
30 Examples:
31
32 >>> qsort([])
33 []
34
35 >>> qsort([3,2,5])
36 [2, 3, 5]
37 """
38
39 #@ Hint: remember that all recursive functions need an exit condition
40 if len(lst) <= 1: #@
41 return lst #@
42
43 #@ Select pivot and apply recursively
44 pivot, rest = lst[0],lst[1:] #@
45 less_than = [ lt for lt in rest if lt < pivot ] #@
46 greater_equal = [ ge for ge in rest if ge >= pivot ] #@
47
48 #@ Upon return, make sure to properly concatenate the output lists
49 return qsort(less_than) + [pivot] + qsort(greater_equal) #@
27def quick_sort(myList, start, end):
28 if start < end:
29 # partitioning the list
30 split = partition(myList, start, end)
31 # split halves & sort both halves
32 quick_sort(myList, start, split - 1)
33 quick_sort(myList, split + 1, end)
34 return myList
5def qsort(lst):
6 """ Quick sort: returns a sorted copy of the list.
7 """
8 # Implement the quick sort logic here
9 return lst
909""" Contains(self: Queue[T], item: T) -> bool """
910pass
38def sort(a):
39 mergeSort(a,0,len(a)-1)
14def shell_sort(collection):
15 """Pure implementation of shell sort algorithm in Python
16 :param collection: Some mutable ordered collection with heterogeneous
17 comparable items inside
18 :return: the same collection ordered by ascending
19
20 >>> shell_sort([0, 5, 3, 2, 2])
21 [0, 2, 2, 3, 5]
22
23 >>> shell_sort([])
24 []
25
26 >>> shell_sort([-2, -5, -45])
27 [-45, -5, -2]
28 """
29 # Marcin Ciura's gap sequence
30 gaps = [701, 301, 132, 57, 23, 10, 4, 1]
31
32 for gap in gaps:
33 i = gap
34 while i < len(collection):
35 temp = collection[i]
36 j = i
37 while j >= gap and collection[j - gap] > temp:
38 collection[j] = collection[j - gap]
39 j -= gap
40 collection[j] = temp
41 i += 1
42
43 return collection
5def quick_sort(arr):
6 less = []
7 pivot_list = []
8 more = []
9 # 递归出口
10 if len(arr) <= 1:
11 return arr
12 else:
13 # 将第一个值做为基准
14 pivot = arr[0]
15 for i in arr:
16 # 将比急转小的值放到less数列
17 if i < pivot:
18 less.append(i)
19 # 将比基准打的值放到more数列
20 elif i > pivot:
21 more.append(i)
22 # 将和基准相同的值保存在基准数列
23 else:
24 pivot_list.append(i)
25 # 对less数列和more数列继续进行排序
26 less = quick_sort(less)
27 more = quick_sort(more)
28 return less + pivot_list + more
23def shell_sort(unsorted_list, add_list):
24 '''shell_sort
25 params:
26 unsorted_list: 未排序列表
27 add_list:增量列表
28 returns:
29 sorted_list:排序列表
30 '''
31 for add in add_list:
32 unsorted_list = shell_sort_once(unsorted_list, add)
33
34 return unsorted_list

Related snippets