I CSDN blog column: HTTPS: //blog.csdn.net/yty_7
Github Address: https: //github.com/yot777/Python-Primary-Learning
5.5 List s [m: n] values
Python list s [m: n] values are from left to right manner
m is the index value of the list of elements, from 0 Start
n is the index value of the list element minus 1
m Always ratio n smaller
Note: m , n- may be a positive, zero, or may be negative.
Simple mnemonic:
From the list s of the m start elements, elements nm taken out.
Test 1
There is a list of s = [ 'a', ' b', 'c', 'd', 'e', 'f']
Try the following value is how much mental arithmetic, and then Python were running comparison
s[0]
s[-2]
s[5]
s[0:5]
s[1:5]
s[-5:-1]
Illustrated as follows:
如果m缺失,变成s[:n],表示从最左边的元素开始,一直取到索引值为n-1的元素
如果n缺失,变成s[m:],表示从索引值为m的元素开始,一直取到最右边的元素
如果m和n都缺失,变成s[:],表示取列表的所有元素
试验2
有一个列表s=['a','b','c','d','e','f']
尝试着心算以下的值是多少,然后和Python运行的结果进行比较
s[2:]
s[:2]
s[-2:]
s[:-2]
s[:]
图解如下:
参考教程:
廖雪峰的Python教程
https://www.liaoxuefeng.com/wiki/1016959663602400
廖雪峰的Java教程
https://www.liaoxuefeng.com/wiki/1252599548343744
Python3 教程 | 菜鸟教程
https://www.runoob.com/python3/
如果您觉得本篇本章对您有所帮助,欢迎关注、评论、点赞!Github欢迎您的Follow、Star!