Python中list的切片细节

简介: Python中的切片功能强大。但是切片很容易让人搞混。个人觉得Python的文档不怎么好,好多东西都是零散的,更像教科书。

Python中的切片功能强大。但是切片很容易让人搞混。

个人觉得Python的文档不怎么好,好多东西都是零散的,更像教科书。

下面的参考来自Python3.2文档和Python参考手册(第4版):

a = [1,2,3,4] x = a[1:2] #a.__getitem__(slice(1,2,None)) slice([start], stop[, step]) Return a slice object representing the set of indices specified by range(start, stop, step). The start and step arguments default to None. Slice objects have read-only data attributes start, stop and step which merely return the argument values (or their default). They have no other explicit functionality; however they are used by Numerical Python and other third party extensions. Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] or a[start:stop, i]. See itertools.islice() for an alternate version that returns an iterator. range([start], stop[, step]) This is a versatile function to create iterables yielding arithmetic progressions. It is most often used in for loops. The arguments must be integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns an iterable of integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised).  

 

可以看到,list的切片,内部是调用__getitem__,和slice函数。而slice函数又是和range()函数相关的。

range([start], stop[, step])

[start, start + step, start + 2 * step, ...]

 

真正让人迷惑的是list[start:stop:step]中的start和stop的默认值。

按《Python参考手册(第4版)》的说法:

    如果不指定start和stop具体值

    当step>0时,start和stop默认值是索引的开头

    当step<0时,start和stop默认值是索引的结尾

我仔细再想下,发现有点不妥,a[::-1]又怎样解释?

我觉得step的符号表示一种方向的含义:

+:即从左向右看,所以start默认是0,stop默认是索引最大值

- :即从右向左看,所以start默认是索引最大值,stop默认是0

如:

 

a = [0,1,2,3,4,5,6,7,8,9] a[:5:-1] #step < 0,所以start = 9 a[0:5:-1] #指定了start = 0 a[1::-1] #step < 0,所以stop = 0 

Python3代码:

l = list(range(10)) print(l[5:0:-1]) print(l[5:4:-1]) print(l[:3:-1]) print(l[0:3:-1]) print(l[9::-1])

输出:

 

[5, 4, 3, 2, 1] [5] [9, 8, 7, 6, 5, 4] [] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] 

相关文章
|
4月前
|
Python
python元组切片
python元组切片
72 3
|
14天前
|
索引 容器
06-python数据容器-list列表定义/list的10个常用操作/列表的遍历/使用列表取出偶数
06-python数据容器-list列表定义/list的10个常用操作/列表的遍历/使用列表取出偶数
|
23天前
|
索引 Python
Python标准数据类型-List(列表)
Python标准数据类型-List(列表)
42 1
|
2月前
|
存储 安全 Java
Python教程第3章 | 集合(List列表、Tuple元组、Dict字典、Set)
Python 列表、无序列表、字典、元组增删改查基本用法和注意事项
51 1
|
2月前
|
存储 数据可视化 索引
Python中List列表的妙用
Python中List列表的妙用
18 0
|
2月前
|
存储 索引 Python
Python中的基础数据结构:列表(List)详解
本文将深入探讨Python中的基础数据结构——列表(List),包括其创建、访问、修改、常用操作以及背后的原理。通过示例代码,帮助读者更好地理解和应用列表。
23 0
|
2月前
|
存储 Python
Python中的列表(list)和元组(tuple)区别
Python中的列表(list)和元组(tuple)区别
28 0
|
2月前
|
数据挖掘 C语言 索引
Python生成列表切片
Python生成列表切片
12 0
|
2月前
|
存储 Python
python列表推导式(List Comprehension)
python列表推导式(List Comprehension)
23 0
|
3月前
|
网络协议 API 开发者
Python 3.9 性能优化:更快的 list()、dict() 和 range() 等内置类型
Python 3.9 性能优化:更快的 list()、dict() 和 range() 等内置类型
13 1