码迷,mamicode.com
首页 > 编程语言 > 详细

Python 倒叙切片

时间:2014-10-23 20:35:17      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:des   io   ar   div   on   art   ad   as   python   

倒序切片

对于list,既然Python支持L[-1]取倒数第一个元素,那么它同样支持倒数切片,试试:

>>> L = [‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Paul‘]

>>> L[-2:]
[‘Bart‘, ‘Paul‘]

>>> L[:-2]
[‘Adam‘, ‘Lisa‘]

>>> L[-3:-1]
[‘Lisa‘, ‘Bart‘]

>>> L[-4:-1:2]
[‘Adam‘, ‘Bart‘]

记住倒数第一个元素的索引是-1。倒序切片包含起始索引,不包含结束索引。

Python 倒叙切片

标签:des   io   ar   div   on   art   ad   as   python   

原文地址:http://www.cnblogs.com/xiaoit/p/4046572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!