python中列表分片

 

>>> test1 = ["aa","bb","cc","dd","ee","ff"]
>>> type(test1)
<class 'list'>
>>> len(test1)
6
>>> test1[0:3]   ##  不包含索引为3的元素
['aa', 'bb', 'cc']
>>> test1[2:4]   ## 不包含索引为4的元素
['cc', 'dd']>>> test1[:4]
['aa', 'bb', 'cc', 'dd']
>>> test1[2:]
['cc', 'dd', 'ee', 'ff']
>>> test1[:]  ## 完整列表
['aa', 'bb', 'cc', 'dd', 'ee', 'ff']
>>> test1[-3:]   ## 取后三个元素
['dd', 'ee', 'ff']

 

上一篇:蛋糕(区间DP)+东方记者(普通DP)


下一篇:2020年蚂蚁金服、头条、拼多多的面试总结!不多BB,直接看!