728x90
반응형
python에서 list, tuple를 자를 때, 효과적인 방법
list[start index
:end index+1
:step
]
>>> list_int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# slice
>>> list_int[:2]
[0, 1]
>>> list_int[0:2]
[0, 1]
>>> list_int[:5]
[0, 1, 2, 3, 4]
>>> list_int[2:5]
[2, 3, 4]
>>> list_int[::1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# even
>>> list_int[::2]
[0, 2, 4, 6, 8]
# odd
>>> list_int[1::2]
[1, 3, 5, 7, 9]
>>> list_int[::3]
[0, 3, 6, 9]
# reverse
>>> list_int[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> list_int[::-2]
[9, 7, 5, 3, 1]
728x90
728x90
BIG
'Programming > Python' 카테고리의 다른 글
named tuple - 네임드듀플 (0) | 2020.09.11 |
---|---|
permutations in itertools (0) | 2020.09.04 |
index(), find() - 원소의 인덱스 (0) | 2020.09.01 |
string unpacking - 문자열 언팩킹 (0) | 2020.08.23 |
copy - shallow, deep : 깊은복사, 얕은복사 (0) | 2020.08.19 |
댓글