>>> x = [1,2,3,4,5,6,7,8] >>> print x [1, 2, 3, 4, 5, 6, 7, 8] >>> print x[::-1] [8, 7, 6, 5, 4, 3, 2, 1]
其他測試︰
>>> x = [1,2,3,4,5,6,7,8] >>> print x [1, 2, 3, 4, 5, 6, 7, 8] >>> print x[::2] #跳一個 [1, 3, 5, 7] >>> print x[::3] #跳兩個 [1, 4, 7] >>> print x[::-2] #反過來跳一個 [8, 6, 4, 2] >>> print x[::-3] #反過來跳兩個 [8, 5, 2] >>> print x[:-1] #由開始到-1 [1, 2, 3, 4, 5, 6, 7] >>> print x[:-3] #由開始到-3 [1, 2, 3, 4, 5] >>> print x[:3] #由開始到3 [1, 2, 3] >>> print x[3:] #由3到最後 [4, 5, 6, 7, 8] >>> print x[-3:] #由-3到最後 [6, 7, 8]
好吧,清晰多了。
No comments:
Post a Comment