← 返回首页
Python3基础教程(十九)
发表时间:2022-03-29 10:55:39
切片

1.切片

所谓切片就是获取一个list或tuple的部分元素的操作。

切片操作基本表达式:object[start_index:end_index:step]

一个完整的切片表达式包含两个“:”,用于分隔三个参数(start_index、end_index、step)。当只有一个“:”时,默认第三个参数step=1;当一个“:”也没有时,start_index=end_index,表示切取start_index指定的那个元素。

参数具体含义:

实例:

# -*- coding: utf-8 -*-
# @Time : 2022/3/29 10:01
# @File : slice.py
# @Software : PyCharm

#引入深拷贝模块
#import copy

fruits = ['grape', 'watermelon', 'apple', 'banana', 'orange']

#当索引只有一个数时,表示切取某一个元素
print(fruits[1])
print(fruits[-3])
#切取完整对象
print(fruits[:]) #从左往右
print(fruits[::-1]) #从左往右

#start_index和end_index全为正(+)索引的情况

print(fruits[1:6:1])
#step=-1,决定了从右往左取值,而start_index=1到end_index=6决定了从左往右取值,两者矛盾,所以为空。
print(fruits[1:6:-1])
#step=1,决定了从左往右取值,而start_index=6到end_index=2决定了从右往左取值,两者矛盾,所以为空。
print(fruits[6:2])
#step=1,表示从左往右取值,而start_index省略时,表示从端点开始,因此这里的端点是“起点”,即从“起点”值0开始一直取到end_index=6(该点不包括)。
print(fruits[:6])
#step=-1,从右往左取值,而start_index省略时,表示从端点开始,因此这里的端点是“终点”,即从“终点”值4开始一直取到end_index=3(该点不包括)。
print(fruits[:3:-1])
#step=1,从左往右取值,从start_index=2开始,一直取到“终点”值4。
print(fruits[2:])

#start_index和end_index全为负(-)索引的情况

#step=1,从左往右取值,而start_index=-1到end_index=-6决定了从右往左取值,两者矛盾,所以为空。
print(fruits[-1:-6])
#step=-1,从右往左取值,start_index=-1到end_index=-6同样是从右往左取值。
print(fruits[-1:-6:-1])
#step=1,从左往右取值,而start_index=-6到end_index=-1同样是从左往右取值。
print(fruits[-6:-1])
#step=1,从左往右取值,从start_index=-6开始,一直取到“终点”
print(fruits[-6:])
#step=-1,从右往左取值,从start_index=-6开始,一直取到“起点”。
print(fruits[-6::-1]);

#start_index和end_index正(+)负(-)混合索引的情况

#start_index=1在end_index=-6的右边,因此从右向左取值,而step=1却决定了从左往右取值,因此结果为空
print(fruits[1:-6])
#start_index=1在end_index=-6的右边,因此从左往右取值,但step=-则决定了从右往左取值,结果正确。
print(fruits[1:-6:-1])

#取偶数位置
print(fruits[::2])
#取奇数位置
print(fruits[1::2])

#拷贝整个对象
temp = fruits[:]
print(temp)
print("fruits id:",id(fruits))
print("temp id:",id(temp))

#或者
temp = fruits.copy()
print(temp)
print("fruits id:",id(fruits))
print("temp id:",id(temp))

#需要注意的是:[:]和.copy()都属于“浅拷贝”,只拷贝最外层元素,内层嵌套元素则通过引用方式共享,而非独立分配内存,如果需要彻底拷贝则需采用“深拷贝”方式。

fruits = ['grape', 'watermelon', 'apple',[100,200,300], 'banana', 'orange']

temp = fruits.copy()
#使用深拷贝,需要引入copy模块
#temp = copy.deepcopy(fruits)
print("fruits id:",id(fruits))
print("temp id:",id(temp)) #id不同说明是不同的对象

#修改外层元素
temp[0]='strawberry'
#尝试修改内层元素
temp[3][0]=10000

print(temp)
#外层元素没有变,但是内存元素也被修改了。
print(fruits)

运行结果:

watermelon
apple
['grape', 'watermelon', 'apple', 'banana', 'orange']
['orange', 'banana', 'apple', 'watermelon', 'grape']
['watermelon', 'apple', 'banana', 'orange']
[]
[]
['grape', 'watermelon', 'apple', 'banana', 'orange']
['orange']
['apple', 'banana', 'orange']
[]
['orange', 'banana', 'apple', 'watermelon', 'grape']
['grape', 'watermelon', 'apple', 'banana']
['grape', 'watermelon', 'apple', 'banana', 'orange']
[]
[]
['watermelon', 'grape']
['grape', 'apple', 'orange']
['watermelon', 'banana']
['grape', 'watermelon', 'apple', 'banana', 'orange']
fruits id: 1598891156672
temp id: 1598891245248
['grape', 'watermelon', 'apple', 'banana', 'orange']
fruits id: 1598891156672
temp id: 1598891269696
fruits id: 1598891270016
temp id: 1598891156672
['strawberry', 'watermelon', 'apple', [10000, 200, 300], 'banana', 'orange']
['grape', 'watermelon', 'apple', [10000, 200, 300], 'banana', 'orange']