导语:
本文主要介绍了关于python列表排序的两种方式的相关知识,包括python运行的两种方式,以及python实现列表排序这些编程知识,希望对大家有参考作用。
1、使用sort()永久排序列表。
使用 sort() 方法更改原始列表。要反转排序,只需将参数 reverse=True 传递给 sort()。
>>> list
['zhangsan', 'lisi', 'bob', 'alex']
>>> list.sort()
>>> list
['alex', 'bob', 'lisi', 'zhangsan']
>>> list.sort(reverse=True)
>>> list
['zhangsan', 'lisi', 'bob', 'alex']
2、用函数sorted()临时排序列表。
sorted() 函数允许列表元素以特定顺序显示,而不影响列表中的原始排序顺序。
若要反转排序,只需将参数reverse=True传送到sorted()。
>>> list = ['douglas','alex','solo','super']
>>> sorted(list)
['alex', 'douglas', 'solo', 'super']
>>> list
['douglas', 'alex', 'solo', 'super']
>>> sorted(list, reverse=True)
['super', 'solo', 'douglas', 'alex']
>>> list
['douglas', 'alex', 'solo', 'super']
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ python自用详解10/20
- ♥ python输入数字到月份09/26
- ♥ python中的枚举函数是什么?09/04
- ♥ python不能使用pip怎么办11/08
- ♥ python库是如何实现对象的转换的12/21
- ♥ C 或 python 用于文本处理01/06
内容反馈