주어진 sequence를 임의의 횟수로 반복하여 순회하고 싶은 경우, itertools.cycle 과 itertools.islice 를 사용할 수 있다.
islice는 iterable 객체에 대한 slice 라고 생각하면 된다.
from itertools import cycle, islice
notes_unit = [1, 2, 3, 4, 5]
islice(iterable, __stop)notes = list(islice(cycle(notes_unit), 3)) print(notes)[1, 2, 3]notes = list(islice(cycle(notes_unit), 8)) print(notes)[1, 2, 3, 4, 5, 1, 2, 3]islice(iterable, __start, __stop)notes = list(islice(cycle(notes_unit), 3, 8)) print(notes)[4, 5, 1, 2, 3]islice(iterable, __start, __stop, __step)notes = list(islice(cycle(notes_unit), 3, 8, 2)) print(notes)[4, 1, 3]
이와 관련된 예제는 다음 문제를 참고.
Q. 2018 KAKAO BLIND RECRUITMENT / [3차] 방금그곡
A. 답안 코드
PREVIOUSEtc