码迷,mamicode.com
首页 > 其他好文 > 详细

切片的用法

时间:2020-04-02 12:00:00      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:切片   复制   使用   style   开始   code   test   lis   sts   

#切片
testStr1 = [1,2,3,4,5]
testStr2 = (1,2,3,4,5)

#正序切片
print(testStr1[0:3])
#[‘1‘, 2, 3]

print(testStr2[:3:2])#每两个取一个
#(‘1‘, 3)

print(testStr2[::5])#每五个取一个数
#(‘1‘,)

print(testStr2[:3])
#(‘1‘, 2, 3)

#倒叙切片
print(testStr1[-3:])
#[3, 4, 5]
print(testStr2[-4:-2])
#(2, 3)

#复制list
testStr2[:]

#字符串也可以看做一种list,可以使用切片
testStr3 = "helloworld"

print(testStr3[-5:])

‘‘‘
testStr[开始位置:取前几个元素]
‘‘‘

 

切片的用法

标签:切片   复制   使用   style   开始   code   test   lis   sts   

原文地址:https://www.cnblogs.com/lvchengda/p/12618540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!