
上QQ阅读APP看书,第一时间看更新
练习4:列表排序
在前面的练习中,生成了一个名为list_1的列表。我们现在来对它进行排序。
1.由于原始列表是一个从0到99的数字列表,所以我们将按相反的顺序对其排序。为此,我们将使用sort方法,设置reverse=True:

部分输出如图1-9所示。
2.直接用reverse方法来达成这个结果:

输出如图1-10所示。

图1-9 反向列表的部分输出

图1-10 反转字符串后的部分输出
说明
sort函数和reverse函数的区别在于,sort的自定义排序功能可以执行自定义排序,而reverse只能用来反转列表。在这里两个函数都是对原对象进行操作,在使用它们时需要注意这一点。