Python – sorted() 和 sort() 之间的区别
排序意味着根据元素上的运算符重新排列给定的元素序列。运算符用于确定相应数据结构中元素的新顺序。
例如:下面的字符列表按其 ASCII 值的升序排序。也就是说,ASCII 值较小的字符将比 ASCII 值较高的字符放在首位。
在Python中,对任何序列进行排序非常容易,因为它提供了内置的排序方法。两个这样的方法是 sorted() 和 sort()。这两种方法用于排序,但它们各自的方式完全不同。让我们一一看看。
排序()
sorted() 方法按升序或降序对给定的序列以及集合和字典(不是序列)进行排序(按字符对字符串char 进行 unicode 比较),并始终返回排序列表。此方法不影响原始序列。
Syntax: sorted(iterable, key, reverse=False)
Parameters:
Iterable: sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted.
Key(optional): A function that would serve as a key or a basis of sort comparison.
Reverse(optional): If set True, then the iterable would be sorted in reverse (descending) order, by default it is set as False.
Return Type: Returns a sorted list.
示例 1:
Python3
# Python program to demonstrate
# sorted()
L = [1, 2, 3, 4, 5]
print("Sorted list:")
print(sorted(L))
print("\nReverse sorted list:")
print(sorted(L, reverse = True))
print("\nOriginal list after sorting:")
print(L)
Python3
# Python program to demonstrate
# sorted()
# List
x = ['q', 'w', 'r', 'e', 't', 'y']
print(sorted(x))
# Tuple
x = ('q', 'w', 'e', 'r', 't', 'y')
print(sorted(x))
# String-sorted based on ASCII translations
x = "python"
print(sorted(x))
# Dictionary
x = {'q':1, 'w':2, 'e':3, 'r':4, 't':5, 'y':6}
print(sorted(x))
# Set
x = {'q', 'w', 'e', 'r', 't', 'y'}
print(sorted(x))
Python3
# Python program to demonstrate
# sorted()
L = ['aaaa', 'bbb', 'cc', 'd']
# sorted without key parameter
print(sorted(L))
print()
# sorted with key parameter
print(sorted(L, key = len))
Python3
# Python program to demonstrate
# sort()
# List of Integers
numbers = [1, 3, 4, 2]
# Sorting list of Integers
numbers.sort()
print(numbers)
# List of Floating point numbers
decimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]
# Sorting list of Floating point numbers
decimalnumber.sort()
print(decimalnumber)
# List of strings
words = ["Geeks", "For", "Geeks"]
# Sorting list of strings
words.sort()
print(words)
Python3
# Python program to demonstrate
# sort()
# List of Integers
numbers = [1, 3, 4, 2]
# Sorting list of Integers
numbers.sort(reverse = True)
print(numbers)
# List of Floating point numbers
decimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]
# Sorting list of Floating point numbers
decimalnumber.sort(reverse = True)
print(decimalnumber)
# List of strings
words = ["Geeks", "For", "Geeks"]
# Sorting list of strings
words.sort(reverse = True)
print(words)
Python3
# Python program to demonstrate sorting by user's
# choice
# function to return the second element of the
# two elements passed as the parameter
def sortSecond(val):
return val[1]
# list1 to demonstrate the use of sorting
# using using second key
list1 = [(1, 2), (3, 3), (1, 1)]
# sorts the array in ascending according to
# second element
list1.sort(key = sortSecond)
print(list1)
# sorts the array in descending according to
# second element
list1.sort(key = sortSecond, reverse = True)
print(list1)
输出:
Sorted list:
[1, 2, 3, 4, 5]
Reverse sorted list:
[5, 4, 3, 2, 1]
Original list after sorting:
[1, 2, 3, 4, 5]
示例 2:对不同的数据类型进行排序
Python3
# Python program to demonstrate
# sorted()
# List
x = ['q', 'w', 'r', 'e', 't', 'y']
print(sorted(x))
# Tuple
x = ('q', 'w', 'e', 'r', 't', 'y')
print(sorted(x))
# String-sorted based on ASCII translations
x = "python"
print(sorted(x))
# Dictionary
x = {'q':1, 'w':2, 'e':3, 'r':4, 't':5, 'y':6}
print(sorted(x))
# Set
x = {'q', 'w', 'e', 'r', 't', 'y'}
print(sorted(x))
输出:
['e', 'q', 'r', 't', 'w', 'y']
['e', 'q', 'r', 't', 'w', 'y']
['h', 'n', 'o', 'p', 't', 'y']
['e', 'q', 'r', 't', 'w', 'y']
['e', 'q', 'r', 't', 'w', 'y']
使用关键参数
这个可选的参数键接受一个函数作为它的值。此键函数在排序之前转换每个元素,它获取值并返回 1 值,然后在排序中使用该值而不是原始值。
示例:假设我们要根据字符串的长度对字符串列表进行排序。这可以通过将 len()函数作为值传递给 key 参数来完成。下面是实现。
Python3
# Python program to demonstrate
# sorted()
L = ['aaaa', 'bbb', 'cc', 'd']
# sorted without key parameter
print(sorted(L))
print()
# sorted with key parameter
print(sorted(L, key = len))
输出:
['aaaa', 'bbb', 'cc', 'd']
['d', 'cc', 'bbb', 'aaaa']
种类()
sort()函数与 sorted() 非常相似,但与 sorted 不同的是,它不返回任何内容并对原始序列进行更改。此外,sort() 是列表类的方法,只能与列表一起使用。
Syntax: List_name.sort(key, reverse=False)
Parameters:
key: A function that serves as a key for the sort comparison.
reverse: If true, the list is sorted in descending order.
Return type: None
示例 1:
Python3
# Python program to demonstrate
# sort()
# List of Integers
numbers = [1, 3, 4, 2]
# Sorting list of Integers
numbers.sort()
print(numbers)
# List of Floating point numbers
decimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]
# Sorting list of Floating point numbers
decimalnumber.sort()
print(decimalnumber)
# List of strings
words = ["Geeks", "For", "Geeks"]
# Sorting list of strings
words.sort()
print(words)
输出:
[1, 2, 3, 4]
[1.68, 2.0, 2.01, 3.28, 3.67]
['For', 'Geeks', 'Geeks']
示例 2:以相反的顺序排序。
Python3
# Python program to demonstrate
# sort()
# List of Integers
numbers = [1, 3, 4, 2]
# Sorting list of Integers
numbers.sort(reverse = True)
print(numbers)
# List of Floating point numbers
decimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]
# Sorting list of Floating point numbers
decimalnumber.sort(reverse = True)
print(decimalnumber)
# List of strings
words = ["Geeks", "For", "Geeks"]
# Sorting list of strings
words.sort(reverse = True)
print(words)
输出:
[4, 3, 2, 1]
[3.67, 3.28, 2.01, 2.0, 1.68]
['Geeks', 'Geeks', 'For']
示例 3:使用 key 参数。
Python3
# Python program to demonstrate sorting by user's
# choice
# function to return the second element of the
# two elements passed as the parameter
def sortSecond(val):
return val[1]
# list1 to demonstrate the use of sorting
# using using second key
list1 = [(1, 2), (3, 3), (1, 1)]
# sorts the array in ascending according to
# second element
list1.sort(key = sortSecond)
print(list1)
# sorts the array in descending according to
# second element
list1.sort(key = sortSecond, reverse = True)
print(list1)
输出:
[(1, 1), (1, 2), (3, 3)]
[(3, 3), (1, 2), (1, 1)]