Python 的List排序
2016-07-20 00:00
260 查看
升序:
方法1:使用list.sort()方法(会修改原始list数据)
>>>L=[2,8,5,10,1]
>>>L.sort()
>>>L
[1,2,5,8,10]
方法2:调用sorted()函数
>>>sorted([2,8,5,10,1])
[1,2,5,8,10]
降序:
>>>L=[2,8,5,10,1]
>>>L.sort(reverse=True)
>>>L
[10,8,5,2,1]
>>>L=[2,8,5,10,1]
>>>sorted(L,reverse=True)
[10,8,5,2,1]
注:1)list.sort()方法只为list定义,而sorted()函数可以接收任何的iterable。
2)list.sort()和sorted()都接收一个reverse参数。它是用于降序排序的标志。
list.sort()不会返回对象,改变原有的list。
附上TABPython中list排序讲解:http://www.pythontab.com/html/2014/pythonjichu_0421/759.html
方法1:使用list.sort()方法(会修改原始list数据)
>>>L=[2,8,5,10,1]
>>>L.sort()
>>>L
[1,2,5,8,10]
方法2:调用sorted()函数
>>>sorted([2,8,5,10,1])
[1,2,5,8,10]
降序:
>>>L=[2,8,5,10,1]
>>>L.sort(reverse=True)
>>>L
[10,8,5,2,1]
>>>L=[2,8,5,10,1]
>>>sorted(L,reverse=True)
[10,8,5,2,1]
注:1)list.sort()方法只为list定义,而sorted()函数可以接收任何的iterable。
2)list.sort()和sorted()都接收一个reverse参数。它是用于降序排序的标志。
两种方法的区别:
sorted(list)返回一个对象,可以用作表达式。原来的list不变,生成一个新的排好序的list对象。list.sort()不会返回对象,改变原有的list。
附上TABPython中list排序讲解:
相关文章推荐
- Python加密—MD5加密
- Python加密——sha1加密
- Python入门之函数式开发
- Python迭代和迭代器详解
- 常用的 Python 爬虫技巧总结
- Python集合使用详解
- 打造自己的 Python 编码环境
- 21行Python代码实现拼写检查器
- 八大排序算法的Python实现
- Python中的 List Comprehension 以及 Generator
- Python greenlet使用介绍及实现原理
- python的super使用
- python3字典、列表和json对象互转
- python(一) A+B
- python3操作mysql数据库增删改查
- python3判断字典、列表、元组为空以及字典是否存在某个key的方法
- python初级项目课(二)
- 【Python语法】python中__name__
- Python学习-1天--基础操作
- python3判断变量类型