全部博文(695)
分类: Python/Ruby
2015-08-19 13:34:21
>>> range(10)[::2] [0, 2, 4, 6, 8]
seq[start:end:step]
>>> range(100)[5:18:2] [5, 7, 9, 11, 13, 15, 17]
>>> s = range(20) >>> s [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> s[::3] [0, 3, 6, 9, 12, 15, 18]
>>> s[2:] [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> s[2::3] [2, 5, 8, 11, 14, 17]
>>> s[5:12] [5, 6, 7, 8, 9, 10, 11] >>> s[5:12:3] [5, 8, 11]
>>> s[:10] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> s[:10:3] [0, 3, 6, 9]
>>> '123123123'[::3] '111'