python list排序sort和sorted的区别
2015-04-15 14:40
681 查看
先上例子,
sort会改变原先的列表,sorted不会改变原列表。
list.sort() 只适用于list类型。sorted适用于任何类型,并返回list类型。
其他的看官方文档。
>>> sorted([5, 2, 3, 1, 4]) [1, 2, 3, 4, 5]
>>> a = [5, 2, 3, 1, 4] >>> a.sort() >>> a [1, 2, 3, 4, 5]
sort会改变原先的列表,sorted不会改变原列表。
list.sort() 只适用于list类型。sorted适用于任何类型,并返回list类型。
>>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}) [1, 2, 3, 4, 5]
其他的看官方文档。
相关文章推荐
- Python List、Dict等的sort与sorted排序
- 【python学习笔记】Python中排序sorted()和list.sort()
- python容器排序sort()和内建函数sorted()的区别
- Python中sort、sorted高级排序技巧
- 【转】python中List的sort方法(或者sorted内建函数)的用法
- Python: sort,sorted,OrderedDict的用法 OrderedDict与Dict的区别
- sorted(),list.sort()函数应用排序字符串的ip地址
- python中List的sort方法(或者sorted内建函数)的用法
- Python排序(sort,sorted)保存原来index
- python中排序,函数sort、sorted、argsort,collections.OrderedDict类
- python中sort()与sorted()区别
- Python 列表排序方法reverse、sort、sorted详解
- python 中 sorted() 和 list.sort() 的用法
- python中List的sort方法(或者sorted内建函数)的用法
- sorted: list sort in python
- 【Python】 sort、sorted高级排序技巧
- [python] list.sort and sorted
- python sort、sorted高级排序技巧
- 【python】 sort、sorted高级排序技巧
- python排序之sort,sorted用法