>>> data=[1,2,3,4,5,6,7,8,9,10] #提取序列的一部分 >>> data[1:6] [2, 3, 4, 5, 6] #提取右側所有 >>> data[3:] [4, 5, 6, 7, 8, 9, 10] >>> data[7:10] [8, 9, 10] #提取左側所有 >>> data[:3] [1, 2, 3] >>> data[0:3] [1, 2, 3] #負索引提取 >>> data[-3:-1] [8, 9] >>> data[-3:] [8, 9, 10] #設置步長 >>> data[::2] [1, 3, 5, 7, 9] >>> data[1::2] [2, 4, 6, 8, 10] #步長也能夠是負數 >>> data[::-1] [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] >>> data[-1:-8:-1] [10, 9, 8, 7, 6, 5, 4] >>> data[8:1:-1] [9, 8, 7, 6, 5, 4, 3]
時常忘記用法,真是窮!html
轉載 http://www.cnblogs.com/moomu/archive/2011/11/14/2247810.htmlpython