赞
踩
aList.sort() #默认是升序排序
aList.sort(reverse = True) #降序排序
aList.sort(key = lambda x:len(str(x))) #按转换成字符串的长度排序
>>> import random
>>> random.shuffle(aList)
#将序列的所有元素随机排序。
使用内置函数sorted对列表进行排序并返回新列表
>>> sorted(aList) #升序排序
>>> sorted(aList,reverse = True) #降序排序
reverse对象方法
>>> aList.reverse() #使用列表对象的reverse方法将元素原地逆序
reversed()函数
使用内置函数reversed方法对列表元素进行逆序排列并返回迭代对象
-
- >>> aList = [3, 4, 5, 6, 7, 9, 11, 13, 15, 17]
- >>> newList = reversed(aList) #返回reversed对象
- >>> list(newList) #把reversed对象转换成列表
- [17, 15, 13, 11, 9, 7, 6, 5, 4, 3]
- >>> for i in newList:
- print(i, end=' ') #这里没有输出内容
- #迭代对象已遍历结束
- >>> newList = reversed(aList) #重新创建reversed对象
- >>> for i in newList:
- print(i, end=' ')
- 17 15 13 11 9 7 6 5 4 3
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。