range用法
使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节。
这里记录一下:
range
(
1
,
5
)
#代表从1到5(不包含5)
[
1
,
2
,
3
,
4
]
range
(
1
,
5
,
2
)
#代表从1到5,间隔2(不包含5)
[
1
,
3
]
range
(
5
)
#代表从0到5(不包含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
:]
#列出0以后的
[
1
,
2
,
5
,
3
,
6
,
8
,
4
]
array[
1
:]
#列出1以后的
[
2
,
5
,
3
,
6
,
8
,
4
]
array[:
-
1
]
#列出-1之前的
[
1
,
2
,
5
,
3
,
6
,
8
]
array[
3
:
-
3
]
#列出3到-3之间的
[
3
]
|