Skip to content

Latest commit

 

History

History
20 lines (16 loc) · 491 Bytes

16 怎么让列表按相反顺序排列?.md

File metadata and controls

20 lines (16 loc) · 491 Bytes

Python100

把Python知识点整理成100道习题,知识点来自两本书:Python基础教程(第3版)和流畅的Python,以后会定期加入更多的习题,大家帮忙点个赞哈,点赞越多,更新越快~

怎么让列表按相反顺序排列?

解法1:用 reverse 方法

>>> num = [1, 22, 45, 99, 49]
>>> num.reverse()
>>> num
[49, 99, 45, 22, 1]

解法2:用切片的方式

>>> num = [1, 22, 45, 99, 49]
>>> num[::-1]
[49, 99, 45, 22, 1]