使用Python的人都知道range()函数和list很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节。这里记录一下range()和list。
- >>> range(1,5)
- [1, 2, 3, 4]
- >>> range(1,5,2)
- [1, 3]
- >>> range(5)
- [0, 1, 2, 3, 4]
再看看list的操作:
- array = [1, 2, 5, 3, 6, 8, 4]
-
- [1, 2, 5, 3, 6, 8, 4]
- (0,1,2,3,4,5,6)
- (-7,-6,-5,-4,-3,-2,-1)
-
- >>> array[0:]
- [1, 2, 5, 3, 6, 8, 4]
- >>> array[1:]
- [2, 5, 3, 6, 8, 4]
- >>> array[:-1]
- [1, 2, 5, 3, 6, 8]
- >>> array[3:-3]
- [3]
-
- >>> array[::2]
- [1, 5, 6, 4]
- >>> array[2::]
- [5, 3, 6, 8, 4]
- >>> array[::3]
- [1, 3, 4]
- >>> array[::4]
- [1, 6]
-
- >>> array[::-1]
- [4, 8, 6, 3, 5, 2, 1]
- >>> array[::-2]
- [4, 6, 5, 1]