공부/PYTHON
[Python] Array[::] (Extended Slices 확장슬라이스)
Grizz
2021. 7. 23. 17:42
728x90
1. 확장 슬라이스
리스트명 [A:B:C]
A : 시작 Index
B : 종료 Index
C : 간격
배열의 Index에 접근하는 방법을 나타낸 것으로 시작부터 종료까지 해당 간격을 가진 배열을 생성한다.
[None Index] A가 비어있다면 처음부터, B가 비어있다면 끝까지, C가 비어있다면 한칸 간격을 의미한다.
2. 예시
#1 list[A:B:C]
ex_list = [1,2,3,4,5,6,7,8,9,10]
print(ex_list[2:9:2]) #[3, 5, 7, 9]
#2 list[:B:C]
print(ex_list[:9:3]) #[1, 4, 7]
#3 list[A::C]
print(ex_list[4::4]) #[5, 9]
#4 list[A:B:]
print(ex_list[1:5:]) #[2, 3, 4, 5]
#5 list[::-1]
#C가 음수인 경우 끝에서 B지점부터 A지점까지 C간격으로 역순 배열 생성
print(ex_list[::-1]) #[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
3. 관련 문서
https://docs.python.org/release/2.3.5/whatsnew/section-slices.html
15 Extended Slices
15 Extended Slices Ever since Python 1.4, the slicing syntax has supported an optional third ``step'' or ``stride'' argument. For example, these are all legal Python syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to Python at the request of the devel
docs.python.org
728x90
반응형