python使用sorted函数对列表进行排序的方法
2019/7/13 21:27:43
本文主要是介绍python使用sorted函数对列表进行排序的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例讲述了python使用sorted函数对列表进行排序的方法。分享给大家供大家参考。具体如下:
python提供了sorted函数用于对列表进行排序,并且可以按照正序或者倒序进行排列
#创建一个数字组成的列表 numbers = [5, 1, 4, 3, 2, 6, 7, 9] #输出排序后的数字数组 print sorted(numbers) #输出原始数组,并未被改变 print numbers my_string = ['aa', 'BB', 'zz', 'CC', 'dd', "EE"] #按字符顺序对字符串列表进行排序,默认为按字符顺序排序 print sorted(my_string) #使用reverse=True按照首字符的倒序进行排序 print sorted(strs, reverse=True) ## ['zz', 'aa', 'CC', 'BB']
希望本文所述对大家的Python程序设计有所帮助。
这篇关于python使用sorted函数对列表进行排序的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python