sorted(iterable, *, key=None, reverse=False)

sorted(iterable, *, key=None, reverse=False)

Built-in Functions
https://docs.python.org/3/library/functions.html

内置函数
https://docs.python.org/zh-cn/3/library/functions.html

Return a new sorted list from the items in iterable.
根据 iterable 中的项返回一个新的已排序列表。

Has two optional arguments which must be specified as keyword arguments.
具有两个可选参数,它们都必须指定为关键字参数。

key specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example, key=str.lower). The default value is None (compare the elements directly).
key 指定带有单个参数的函数,用于从 iterable 的每个元素中提取用于比较的键 (例如 key=str.lower)。默认值为 None (直接比较元素)。

reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.
reverse 为一个布尔值。如果设为 True,则每个列表元素将按反向顺序比较进行排序。

iterable - 可迭代对象。
key - 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse - 排序规则,reverse = True 降序, reverse = False 升序 (默认)。

Use functools.cmp_to_key() to convert an old-style cmp function to a key function.
使用 functools.cmp_to_key() 可将老式的 cmp 函数转换为 key 函数。

The built-in sorted() function is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).
内置的 sorted() 确保是稳定的。如果一个排序确保不会改变比较结果相等的元素的相对顺序就称其为稳定的 - 这有利于进行多重排序 (例如先按部门、再按薪级排序)。

For sorting examples and a brief sorting tutorial, see Sorting HOW TO.
有关排序示例和简要排序教程,请参阅排序指南。

1. Python 2.x

sorted() 函数对所有可迭代的对象进行排序操作。
sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
reverse = True 降序,reverse = False 升序 (默认)。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

fx = [5, 7, 6, 3, 4, 1, 2]
fy = sorted(fx)
fy_false = sorted(fx, reverse=False)
fy_true = sorted(fx, reverse=True)

print("fx", fx)
print("fy", fy)
print("fy_false", fy_false)
print("fy_true", fy_true)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
fx [5, 7, 6, 3, 4, 1, 2]
fy [1, 2, 3, 4, 5, 6, 7]
fy_false [1, 2, 3, 4, 5, 6, 7]
fy_true [7, 6, 5, 4, 3, 2, 1]

Process finished with exit code 0

2. Python 3.x

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

fx = [5, 7, 6, 3, 4, 1, 2]
fy = sorted(fx)
fy_false = sorted(fx, reverse=False)
fy_true = sorted(fx, reverse=True)

print("fx", fx)
print("fy", fy)
print("fy_false", fy_false)
print("fy_true", fy_true)
/usr/bin/python3.5 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
fx [5, 7, 6, 3, 4, 1, 2]
fy [1, 2, 3, 4, 5, 6, 7]
fy_false [1, 2, 3, 4, 5, 6, 7]
fy_true [7, 6, 5, 4, 3, 2, 1]

Process finished with exit code 0

3. sorted()

利用 key 进行倒序排序。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

fx = [5, 0, 6, 1, 2, 7, 3, 4]
fy = sorted(fx, key=lambda x: x * -1)
print("fy", fy)

fy_vs = sorted(fx, key=lambda x: x * 1)
print("fy_vs", fy_vs)
/usr/bin/python3.5 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
fy [7, 6, 5, 4, 3, 2, 1, 0]
fy_vs [0, 1, 2, 3, 4, 5, 6, 7]

Process finished with exit code 0

4. sorted()

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

dict_data = {1: 'D', 6: 'B', 3: 'B', 8: 'E', 5: 'A', 7: 'K', 8: 'M'}

sorted_data = sorted(dict_data)
print("dict_data", dict_data)
print("sorted_data", sorted_data)
/usr/bin/python3.5 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
dict_data {1: 'D', 3: 'B', 5: 'A', 6: 'B', 7: 'K', 8: 'M'}
sorted_data [1, 3, 5, 6, 7, 8]

Process finished with exit code 0

5. sorted()

sorted 的应用,通过 key 的值来进行数组/字典的排序。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

array = [{"age": 20, "name": "a"}, {"age": 25, "name": "b"}, {"age": 10, "name": "c"}]
array_data = sorted(array, key=lambda x: x["age"])

print("array", array)
print("array_data", array_data)
/usr/bin/python3.5 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
array [{'age': 20, 'name': 'a'}, {'age': 25, 'name': 'b'}, {'age': 10, 'name': 'c'}]
array_data [{'age': 10, 'name': 'c'}, {'age': 20, 'name': 'a'}, {'age': 25, 'name': 'b'}]

Process finished with exit code 0

6. sorted()

先按照成绩降序排序,相同成绩的按照名字升序排序:

# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

dict_data = [{'name': 'alice', 'score': 38}, {'name': 'bob', 'score': 18}, {'name': 'darl', 'score': 28},
             {'name': 'christ', 'score': 28}]
dict_sorted = sorted(dict_data, key=lambda x: (-x['score'], x['name']))

print("dict_data", dict_data)
print("dict_sorted", dict_sorted)
/usr/bin/python3.5 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
dict_data [{'name': 'alice', 'score': 38}, {'name': 'bob', 'score': 18}, {'name': 'darl', 'score': 28}, {'name': 'christ', 'score': 28}]
dict_sorted [{'name': 'alice', 'score': 38}, {'name': 'christ', 'score': 28}, {'name': 'darl', 'score': 28}, {'name': 'bob', 'score': 18}]

Process finished with exit code 0
展开阅读全文

没有更多推荐了,返回首页