python基础教程——切片
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础教程——切片相关的知识,希望对你有一定的参考价值。
获取list或tuple的部分元素:
L = [‘Michael‘, ‘Sarah‘, ‘Tracy‘, ‘Bob‘, ‘Jack‘] L[0:3] [‘Michael‘, ‘Sarah‘, ‘Tracy‘] L[:3] [‘Michael‘, ‘Sarah‘, ‘Tracy‘] L[-2:] [‘Bob‘, ‘Jack‘] L[-2:-1] [‘Bob‘] L = list(range(100)) L [0,1,2,3,4,5,...,99] L[:10:2] [0,2,4,6,8] L[::5] [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95] L[:] [0, 1, 2, 3, ..., 99] //复制一个list (0,1,2,3)[:3] (0,1,2) ‘ABCDEFG‘[:3] ‘ABC‘ ‘ABCDEFG‘[::2] ‘ACEG‘
以上是关于python基础教程——切片的主要内容,如果未能解决你的问题,请参考以下文章