python数据类型-列表

列表使用:切片
切片操作基本表达式:[start_index:stop_index:step] start 值:
(1)start_index,如果没有指定,则默认开始值为 0;
(2)stop_index 值: 指示到哪个索引值结束,但不包括这个结束索引值。如果没有指定,则取列表允许的最大索引值(即list.length);
(3)step 值: 步长值指示每一步大小,如果没有指定,则默认步长值为 1。
(4)当 step>0,start_index 的空值下标为 0,stop_index 为空时,值下标为list.length,step 的方向是左到右;
(5)当 step<0,start_index 的空值下标为list.length,stop_index 的空值下标为 0,此时反向为右到左
三个值都是可选的,非必填

访问列表中的值
使用下标索引来访问列表中的值,同样你也可以使用方括号的形式截取字符

索引
默认正向索引,编号从 0 开始。
支持反向索引,编号从-1 开始。

列表切片示意图:


索引包括正索引和负索引两部分,如上图所示,以list对象list = [“red”,“green”,“blue”,“yellow”,“white”,“black”]为例:

1. 获取单个元素
list = ["red","green","blue","yellow","white","black"]
#red
print(list[0])
# black
print(list[-1])
1
2
3
4
5
可以通过列表的索引获取列表的值

2.获取列表对象
list = ["red","green","blue","yellow","white","black"]
'''
从左往右获取索引,['red', 'green', 'blue', 'yellow', 'white', 'black']
'''
print(list[:])
print(list[::])
print(list[::1])
'''
从右往左获取索引(反向索引),['black', 'white', 'yellow', 'blue', 'green', 'red']
'''
print(list[::-1])

1
2
3
4
5
6
7
8
9
10
11
12
3.获取列表部分的值
列表切片遵循的是前闭后开原则(意思是最后一个索引对应的值是获取不到)

(1)正向索引

'''
list = ["red","green","blue","yellow","white","black"]
"""
正向索引:step为正数
"""
# 正向索引:start_index为0到end_index为6
print(list[0:6]) # ['red', 'green', 'blue', 'yellow', 'white', 'black']

# start_index没有填写,默认从第一个开始,一直取到end_index=6
print(list[:6]) # ['red', 'green', 'blue', 'yellow', 'white', 'black']

# step没有填写,默认是1,start_index为0,一直取到end_index=2
print(list[0:2])  #['red', 'green']

# step没有填写,默认是1,start_index为1,一直取到end_index=4
print(list[1:4])  # ['green', 'blue', 'yellow']

# start_index为1,一直取到end_index=5,step是2
print(list[1:5:2])  # ['green', 'yellow']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(2)反向索引

'''
反向索引:step为负数
'''
print("反向索引=============")
# step=1,反向索引,从start_index=-6开始,一直取到end_index=0为止。
print(list[-6::])  #['red', 'green', 'blue', 'yellow', 'white', 'black']

# step=-1,反向索引,从start_index=3开始,一直取到end_index=0为止。
print(list[3:0:-1])  #['yellow', 'blue', 'green']

# step=-2,反向索引,从start_index=6开始,一直取到end_index=0为止。
print(list[6::-2])  #['black', 'yellow', 'green']

# step=-3,反向索引,从start_index=5开始,一直取到end_index=2为止。
print(list[5:2:-3])  #['black']

# step=-1,反向索引,从start_index=-3开始,一直取到end_index=-5为止。
print(list[-3:-5:-1])  #['yellow', 'blue']

# start_index > end_index时,取出的结果为空
print(list[4:2])  #[]
print(list[-5:-3:-1])  # []
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
4、列表多层切片
'''多层切片'''
list = ["red","green","blue","yellow","white","black"]
# 链式列表切片
print(list[:6][2:5][-1:])

'''上边的链式列表与下边的步骤是相等的'''
list2 = list[:6]
# ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(list2)
list3 = list2[2:5]
# ['blue', 'yellow', 'white']
print(list3)
list4 = list3[-1:]
# ['white']
print(list4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

————————————————
版权声明:本文为CSDN博主「橙子软件测试菇凉」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43911915/article/details/123905267

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值