Python标准库:内置函数sorted(iterable[, key][, reverse])
2015-04-19 13:36
447 查看
本函数是实现对可迭代对象iterable进行排序。可选参数key是比较键的函数;reverse是表示是否反向排列对象里的项,是布尔值。
例子:
结果输出如下:
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
[('jane', 'B', 62), ('john', 'A', 75), ('dave', 'B', 100)]
蔡军生 QQ:9073204 深圳
例子:
#sorted() print(sorted([5, 2, 3, 1, 4])) print(sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}, reverse = True)) print(sorted("This is a test string from Andrew".split(), key=str.lower)) student_tuples = [ ('john', 'A', 75), ('jane', 'B', 62), ('dave', 'B', 100), ] print(sorted(student_tuples, key=lambda student: student[2])) # 按年龄排序
结果输出如下:
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
[('jane', 'B', 62), ('john', 'A', 75), ('dave', 'B', 100)]
蔡军生 QQ:9073204 深圳
相关文章推荐
- Python标准库:内置函数sorted(iterable[, key][, reverse])
- Python标准库:内置函数tuple([iterable])
- Python标准库:内置函数max(iterable, *[, key, default])
- Python标准库:内置函数filter(function, iterable)
- python sorted()函数cmp,key,reverse参数详解
- Python标准库:内置函数max(iterable, *[, key, default])
- Python标准库:内置函数filter(function, iterable)
- Python标准库:内置函数min(iterable, *[, key, default])
- Python标准库:内置函数tuple([iterable])
- Python标准库:内置函数dict(iterable, **kwarg)
- Python标准库:内置函数min(iterable, *[, key, default])
- Python标准库:内置函数tuple([iterable])
- Python标准库:内置函数dict(iterable, **kwarg)
- Python标准库:内置函数list([iterable])
- Python标准库:内置函数list([iterable])
- Python标准库:内置函数all(iterable)
- Python标准库:内置函数frozenset([iterable])
- [转载]Python标准库:内置函数sum(iterable[, start])
- Python标准库:内置函数all(iterable)
- Python标准库:内置函数frozenset([iterable])