파이썬[Python]: itertools - repeat 함수

파이썬(Phthon): itertools 모듈 - repeat 함수(function)


/// 설명

인자 object를 계속 반복합니다. times 인자가 있을 경우에는 times만큼 반복됩니다. map() 이나 zip()에 자주 사용됩니다.

※ 형식
itertools.repeat(object[, times])

reference
https://docs.python.org/3/library/itertools.html#module-itertools

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import itertools
 
test_repeat = itertools.repeat(2)
print(next(test_repeat))  # 2
print(next(test_repeat))  # 2
print(next(test_repeat))  # 2
# print(next(test_repeat))
# ...
# ... endless
 
test_pow = map(pow, range(10), itertools.repeat(2))
print(list(test_pow))
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
 
test_list = []
 
for i in itertools.repeat(05):
    test_list.append(i)
 
test_list.append(1)
 
for i in itertools.repeat(05):
    test_list.append(i)
 
print(test_list)
# [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수