码迷,mamicode.com
首页 > 编程语言 > 详细

python基础教程——切片

时间:2017-09-15 01:44:32      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:复制   ...   部分   logs   基础   blog   sar   python   log   

获取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基础教程——切片

标签:复制   ...   部分   logs   基础   blog   sar   python   log   

原文地址:http://www.cnblogs.com/helloluo/p/7523529.html

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