🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Python `sorted()`函数 > 原文: [https://thepythonguru.com/python-builtin-functions/sorted/](https://thepythonguru.com/python-builtin-functions/sorted/) * * * 于 2020 年 1 月 7 日更新 * * * `sorted()`内置函数允许我们对数据进行排序。 它接受一个可迭代对象,并返回一个包含来自可迭代对象的项目的排序列表。 默认情况下,它以升序排序。 `sorted()`函数的语法如下: **语法**:`sorted(iterable, key=None, reverse=False)` | 参数 | 描述 | | --- | --- | | `iterable` | (必需)可迭代地进行排序,例如字符串,列表,字典,元组等。 | | `key`,(可选) | 它引用单个参数函数以自定义排序顺序。 该函数应用于迭代器上的每个项目。 默认情况下,此参数设置为`None`。 | | `reverse` | (可选)布尔值标志以反转排序顺序。 默认为`False`。 | 如果可迭代对象中的项目是字符串,则将按字母顺序对其进行排序。 另一方面,如果它们是数字,则将按数字顺序对其进行排序。 这是一个例子: ```py >>> >>> fruits = ['lime', 'blueberry', 'plum', 'avocado'] >>> >>> sorted(fruits) # default sorting, in ascending order ['avocado', 'blueberry', 'lime', 'plum'] >>> >>> >>> sorted(fruits, reverse=True) # reverse the sorting ['plum', 'lime', 'blueberry', 'avocado'] >>> >>> >>> ages = [45, 11, 30, 20, 55] >>> >>> sorted(ages) [11, 20, 30, 45, 55] >>> >>> sorted(ages, reverse=True) [55, 45, 30, 20, 11] >>> ``` 试试看: ```py fruits = ['lime', 'blueberry', 'plum', 'avocado'] print(sorted(fruits)) # default sorting, in ascending order print(sorted(fruits, reverse=True)) # reverse the sorting ages = [45, 11, 30, 20, 55] print(sorted(ages)) print(sorted(ages, reverse=True)) ``` 请注意,`sorted()`返回一个包含可迭代项的新列表。 它不会更改过程中的原始可迭代项。 ```py >>> >>> fruits # fruit list is same as before ['lime', 'blueberry', 'plum', 'avocado'] >>> >>> >>> ages # ages list is same as before [45, 11, 30, 20, 55] >>> ``` 试一试: ```py fruits = ['lime', 'blueberry', 'plum', 'avocado'] ages = [45, 11, 30, 20, 55] print(sorted(fruits)) # default sorting, in ascending order print(sorted(fruits, reverse=True)) # reverse the sorting print(fruits) print(ages) ``` 以下是一些其他示例,显示`sorted()`如何与其他 Python 类型一起使用。 ## 带字符串的`sorted()` * * * ```py >>> >>> name = "Alfred Hajos" >>> >>> sorted(name) [' ', 'A', 'H', 'a', 'd', 'e', 'f', 'j', 'l', 'o', 'r', 's'] >>> >>> >>> sorted(name, reverse=True) ['s', 'r', 'o', 'l', 'j', 'f', 'e', 'd', 'a', 'H', 'A', ' '] >>> ``` 试一试: ```py name = "Alfred Hajos" print(sorted(name)) print(sorted(name, reverse=True)) ``` 请注意,在第一个`sorted()`调用`A`的结果出现在`a`之前。 这是因为`A`的 ASCII 值为 65,`a`的 ASCII 值为 97。出于相同的原因,空格字符(`' '`)的 ASCII 值 32 出现在`A`之前。 ## 带元组的`sorted()` * * * ```py >>> >>> t = ( 'ff', 'xx', 'gg', 'aa') >>> >>> sorted(t) ['aa', 'ff', 'gg', 'xx'] >>> >>> sorted(t, reverse=True) ['xx', 'gg', 'ff', 'aa'] >>> ``` 试一试: ```py t = ( 'ff', 'xx', 'gg', 'aa') print(sorted(t)) print(sorted(t, reverse=True)) ``` ## 带字典的`sorted()` * * * ```py >>> >>> d = {'name': 'John', 'age': 25, 'designation': 'manager'} >>> >>> >>> sorted(d) ['age', 'designation', 'name'] >>> >>> sorted(d, reverse=True) ['name', 'designation', 'age'] >>> >>> >>> for k in sorted(d): ... print(k, d[k]) ... age 25 designation manager name John >>> >>> >>> for k in sorted(d, reverse=True): ... print(k, d[k]) ... name John designation manager age 25 >>> ``` ```py d = {'name': 'John', 'age': 25, 'designation': 'manager'} print(sorted(d)) print(sorted(d, reverse=True)) for k in sorted(d): print(k, d[k]) print('-'*10) for k in sorted(d, reverse=True): print(k, d[k]) ``` ## 使用命名参数`key`自定义排序顺序 * * * 从上一节中我们知道,如果将`sorted()`函数应用于字符串列表,则将获得按字母顺序排序的字符串列表。 如果我们想按字符串的长度而不是字母顺序排序怎么办? 这是`key`命名参数出现的地方。 要按字符串长度排序,请按以下所示将`len()`函数的键命名参数设置为: ```py >>> >>> fruits ['lime', 'blueberry', 'plum', 'avocado'] >>> >>> sorted(fruits) # sort by alphabetical order ['avocado', 'blueberry', 'lime', 'plum'] >>> >>> sorted(fruits, key=len) # sort by string length ['lime', 'plum', 'avocado', 'blueberry'] >>> >>> sorted(fruits, key=len, reverse=True) # reverse sort order ['blueberry', 'avocado', 'lime', 'plum'] >>> ``` 试一试: ```py fruits = ['lime', 'blueberry', 'plum', 'avocado'] print(fruits) print(sorted(fruits)) print(sorted(fruits, key=len)) # sort by string length print(sorted(fruits, key=len, reverse=True)) # reverse sort order ``` 有时您可能希望使排序不区分大小写。 我们可以通过将`key`的命名参数设置为`str.lower`函数来轻松实现此目的。 ```py >>> >>> t = ( 'AA', 'aa', 'ZZ', 'cc', 'bb') >>> >>> sorted(t) ['AA', 'ZZ', 'aa', 'bb', 'cc'] >>> >>> sorted(t, key=str.lower) ['AA', 'aa', 'bb', 'cc', 'ZZ'] >>> ``` ```py t = ( 'AA', 'aa', 'ZZ', 'cc', 'bb') print(sorted(t)) print(sorted(t, key=str.lower)) ``` 这是另一个示例,该示例使用自定义函数根据其包含的元音数量对字符串列表进行排序。 ```py >>> >>> fruits ['lime', 'blueberry', 'plum', 'avocado'] >>> >>> >>> def count_vowel(s): ... vowels = ('a', 'e', 'i', 'o', 'u') ... count = 0 ... ... for i in s: ... if i in vowels: ... count = count + 1 ... ... return count ... >>> >>> >>> sorted(fruits) ['avocado', 'blueberry', 'lime', 'plum'] >>> >>> sorted(fruits, key=count_vowel) ['plum', 'lime', 'blueberry', 'avocado'] >>> ``` 试一试: ```py fruits = ['lime', 'blueberry', 'plum', 'avocado'] def count_vowel(s): vowels = ('a', 'e', 'i', 'o', 'u') count = 0 for i in s: if i in vowels: count = count + 1 return count print(sorted(fruits)) print(sorted(fruits, key=count_vowel)) ``` 您还可以在用户定义的对象上使用`sorted()`。 ```py >>> >>> class Employee: ... def __init__(self, name, salary, age): ... self.name = name ... self.salary = salary ... self.age = age ... ... def __repr__(self): ... return self.__str__() ... ... def __str__(self): ... return "{0}:{1}:{2}".format(self.name, self.salary, self.age) ... >>> >>> >>> e1 = Employee("Tom", 20000, 32) >>> e2 = Employee("Jane", 50000, 36) >>> e3 = Employee("Bob", 45000, 40) >>> >>> >>> emp_list = [e2, e3, e1] >>> >>> print(emp_list) [Jane:50000:36, Bob:45000:40, Tom:20000:32] >>> ``` ```py class Employee: def __init__(self, name, salary, age): self.name = name self.salary = salary self.age = age def __repr__(self): return self.__str__() def __str__(self): return "{0}:{1}:{2}".format(self.name, self.salary, self.age) e1 = Employee("Tom", 20000, 32) e2 = Employee("Jane", 50000, 36) e3 = Employee("Bob", 45000, 40) emp_list = [e2, e3, e1] print(emp_list) # print(sorted(emp_list)) ``` 如果现在在`emp_list`上调用`sorted()`,则会出现如下错误: ```py >>> >>> sorted(emp_list) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: Employee() < Employee() >>> ``` **提示**: 上面的代码不会在 Python 2 中引发任何错误。相反,它将根据`id()`内置函数返回的 ID 对`Employee`对象进行排序。 发生这种情况是因为 Python 不知道如何比较`Employee`对象。 我们可以通过在`Employee`类中实现[特殊方法](/python-operator-overloading/)(例如`__lt__()`,`__gt__()`等)来告诉 Python 如何比较对象。 代替定义特殊方法,我们可以显式告诉`sorted()`函数如何使用`key`命名参数对`Employee`对象进行排序。 ```py >>> >>> sorted(emp_list, key=lambda x: x.name) # sort Employee objects by name [Bob:45000:40, Jane:50000:36, Tom:20000:32] >>> >>> >>> sorted(emp_list, key=lambda x: x.age) # sort Employee objects by age [Tom:20000:32, Jane:50000:36, Bob:45000:40] >>> >>> >>> print(sorted(emp_list, key=lambda x: x.salary)) # sort Employee objects by salary [Tom:20000:32, Bob:45000:40, Jane:50000:36] >>> ``` ```py class Employee: def __init__(self, name, salary, age): self.name = name self.salary = salary self.age = age def __repr__(self): return self.__str__() def __str__(self): return "{0}:{1}:{2}".format(self.name, self.salary, self.age) e1 = Employee("Tom", 20000, 32) e2 = Employee("Jane", 50000, 36) e3 = Employee("Bob", 45000, 40) emp_list = [e2, e3, e1] print(sorted(emp_list, key=lambda x: x.name)) # sort Employee objects by name print(sorted(emp_list, key=lambda x: x.age)) # sort Employee objects by age print(sorted(emp_list, key=lambda x: x.salary)) # sort Employee objects by salary ``` * * * * * *