理解和使用Python中的sorted()函数
Python中的sorted()函数是用于对可迭代对象进行排序操作的内置函数,在使用时需要注意一些细节。本文将详细介绍sorted()函数的使用方法,以及常见的一些应用场景和注意事项。
1. sorted()函数的基本用法
sorted()函数的基本语法如下:
sorted(iterable, key=None, reverse=False)
参数说明:
- iterable:表示待排序的可迭代对象,例如列表、元组、字典等。
- key:表示排序时要根据哪个属性进行排序(默认为None),可以是一个函数或lambda表达式。
- reverse:表示是否降序排序(默认为False),如果为True,则表示倒序排序。
具体来说,当sorted()函数只有一个参数时,表示对该可迭代对象进行升序排序,例如:
>>> nums = [4, 2, 7, 1, 9, 5]
>>> sorted(nums)
[1, 2, 4, 5, 7, 9]
当需要进行降序排序时,可以在sorted()函数中设置reverse=True参数,例如:
>>> nums = [4, 2, 7, 1, 9, 5]
>>> sorted(nums, reverse=True)
[9, 7, 5, 4, 2, 1]
当需要根据某个属性进行排序时,可以在sorted()函数中设置key参数,例如:
>>> students = [
... {'name': 'Tom', 'score': 85},
... {'name': 'Lucy', 'score': 92},
... {'name': 'Jack', 'score': 79},
... {'name': 'Mary', 'score': 95}
... ]
>>> sorted(students, key=lambda x: x['score'])
[{'name': 'Jack', 'score': 79},
{'name': 'Tom', 'score': 85},
{'name': 'Lucy', 'score': 92},
{'name': 'Mary', 'score': 95}]
2. sorted()函数的常见应用场景
sorted()函数是Python中非常常用的函数之一,可以用于多种排序场景,例如:
- 对数字列表进行排序:sorted()函数可以轻松地对数字列表进行排序,例如:
>>> nums = [4, 2, 7, 1, 9, 5]
>>> sorted(nums)
[1, 2, 4, 5, 7, 9]
- 对字典列表根据某个属性进行排序:sorted()函数可以方便地对字典列表根据某个属性进行排序,例如:
>>> students = [
... {'name': 'Tom', 'score': 85},
... {'name': 'Lucy', 'score': 92},
... {'name': 'Jack', 'score': 79},
... {'name': 'Mary', 'score': 95}
... ]
>>> sorted(students, key=lambda x: x['score'])
[{'name': 'Jack', 'score': 79},
{'name': 'Tom', 'score': 85},
{'name': 'Lucy', 'score': 92},
{'name': 'Mary', 'score': 95}]
- 对字符串列表进行排序:sorted()函数还可以对字符串列表进行排序,例如:
>>> names = ['Tom', 'Lucy', 'Jack', 'Mary']
>>> sorted(names)
['Jack', 'Lucy', 'Mary', 'Tom']
- 对复杂对象进行排序:sorted()函数还可以对复杂对象进行排序,例如:
>>> class Student:
... def __init__(self, name, score):
... self.name = name
... self.score = score
...
>>> s1 = Student('Tom', 85)
>>> s2 = Student('Lucy', 92)
>>> s3 = Student('Jack', 79)
>>> s4 = Student('Mary', 95)
>>> students = [s1, s2, s3, s4]
>>> sorted(students, key=lambda x: x.score)
[<__main__.Student object at 0x7ff8d8c78ac0>,
<__main__.Student object at 0x7ff8d8c78be0>,
<__main__.Student object at 0x7ff8d8c78a90>,
<__main__.Student object at 0x7ff8d8c78c10>]
3. 注意事项
在使用sorted()函数时,需要注意一些细节问题,例如:
- sorted()函数只是返回一个排好序的新列表,原有的列表并不会发生改变。
- 如果需要对列表本身进行排序,则可以使用列表的sort()方法,例如:
>>> nums = [4, 2, 7, 1, 9, 5]
>>> nums.sort() # 对原有列表进行排序
>>> nums
[1, 2, 4, 5, 7, 9]
- 当需要按照多个属性进行排序时,可以使用多级排序,例如:
>>> students = [
... {'name': 'Tom', 'score': 85, 'age': 19},
... {'name': 'Lucy', 'score': 92, 'age': 20},
... {'name': 'Jack', 'score': 79, 'age': 20},
... {'name': 'Mary', 'score': 95, 'age': 19}
... ]
>>> sorted(students, key=lambda x: (x['age'], x['score']))
[{'name': 'Mary', 'score': 95, 'age': 19},
{'name': 'Tom', 'score': 85, 'age': 19},
{'name': 'Jack', 'score': 79, 'age': 20},
{'name': 'Lucy', 'score': 92, 'age': 20}]
- 当需要按照自定义函数进行排序时,可以在函数中返回一个元组作为排序依据,例如:
>>> def my_sort(x):
... return x[0], -x[1]
...
>>> nums = [(2, 3), (1, 1), (2, 2), (1, 4), (3, 1)]
>>> sorted(nums, key=my_sort)
[(1, 4), (1, 1), (2, 3), (2, 2), (3, 1)]
总之,sorted()函数是Python中强大而实用的一个函数,能够轻松地实现各种排序功能。希望本文能够帮助大家更好地理解和掌握sorted()函数的使用方法。
