라벨이 itertools인 게시물 표시

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

파이썬(Phthon): itertools 모듈 - tee 함수(function) /// 설명 하나의 반복 가능한 객체를 n개의 독립된 이터레이터로 만들어 줍니다. 만약 tee() 함수에 의해 분리되어지면, 최초의 반복 가능한 객체는 변경하지 않는 것이 좋습니다. orginal iterable != splited iterator ※ 형식 itertools.tee(iterable, n=2) 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 import  itertools   test_list  =  [( 1 ,  2 ), ( 3 ,  4 ), ( 5 ,  6 )]   test_tee  =  itertools.tee(test_list,  2 ) print (test_tee)   # (<itertools._tee object at 0x00000233D60CA4C0>, <itertools._tee object at 0x00000233D60CA500>)   test_x, test_y  =  test_tee print (test_x)   # <itertools._tee object at 0x00000233D60CA4C0> print (test_y)   # <itertools._tee object at 0x00000233D60CA500>   print (list(test_x))   # [(1, 2), (3, 4), (5, 6)] print (list(test_y))   # [(1, 2), (3, 4), (5, 6)]   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - takewhile 함수(function) /// 설명 반복 가능한 객체의 요솟값을 적용한 predicate의 반환값이 True 인 경우까지의 요소들을 이터레이터로 만들어 줍니다. ※ 형식 itertools.takewhile(predicate, iterable) reference https://docs.python.org/3/library/itertools.html#module-itertools /// 예제 1 2 3 4 5 6 7 8 import  itertools   test_pd  =  lambda x: x  <   3 test_list  =  [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ]   test_dh  =  itertools.takewhile(test_pd, test_list) print (list(test_dh))   # [1, 2]   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - starmap 함수(function) /// 설명 반복가능한 객체(iterable)의 요소들을 function에 적용시킨 결과를 반환합니다. 하나의 반복 가능한 객체가 튜플에 의해 그룹지어져 있을 때 사용합니다. ※ 형식 itertools.starmap(function, iterable) reference https://docs.python.org/3/library/itertools.html#module-itertools /// 예제 1 2 3 4 5 6 7 import  itertools   test_x  =  [( 1 ,  5 ), ( 2 ,  1 ), ( 3 ,  7 )]   test_sm  =  itertools.starmap(max, test_x) print (list(test_sm))   # [5, 2, 7]   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - product 함수(function) /// 설명 반복가능한 객체(iterable)의 곱집합을 만들어 줍니다. ※ 형식 itertools.product(*iterables, repeat=1) 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 import  itertools   test_x  =   'ABC' test_y  =   'xy'   test_p  =  itertools.product(test_x, test_y) print (list(test_p))   # [('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y'), ('C', 'x'), ('C', 'y')]   test_xy  =  [(x, y)  for  x  in  test_x  for  y  in  test_y] print (test_xy)   # [('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y'), ('C', 'x'), ('C', 'y')]   test_p  =  itertools.product(test_y, repeat = 2 ) print (list(test_p))   # [('x', 'x'), ('x', 'y'), ('y', 'x'), ('

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

파이썬(Phthon): itertools 모듈 - pairwise 함수(function) /// 설명 반복 가능한 객체로 부터 2개의 요소를 가진 튜플들이 만들어 집니다.(overlapping) 'abcd' -> (a, b), (b, c), (c, d) ※ 형식 itertools.pairwise(iterable) reference https://docs.python.org/3/library/itertools.html#module-itertools /// 예제 1 2 3 4 5 6 7 import  itertools   test  =   '1123411'   test_pw  =  itertools.pairwise(test) print (list(test_pw))   # [('1', '1'), ('1', '2'), ('2', '3'), ('3', '4'), ('4', '1'), ('1', '1')]   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.10) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - permutations 함수(function) /// 설명 반복가능한 객체(iterable)에서 r 길이를 가진 서브 시퀀스들을 반환합니다.(순열:permutation) r이 주어지지 않으면, 객체의 길이가 적용되어 집니다. ※ 형식 itertools.permutations(iterable, r=None) 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 import  itertools   test_1  =   'ab' test_2  =  [ 1 ,  2 ,  3 ]   test_c  =  itertools.permutations(test_1,  2 ) test_sorted  =  sorted(test_c)   for  i  in  test_sorted:      print (i, end = '' )   # ('a', 'b')('b', 'a')   print ()   test_c  =  itertools.permutations(test_2,  3 ) test_sorted  =  sorted(test_c)   for  i  in  test_sorted:      print (i, end = '' )      # (1, 2, 3)(1, 3, 2)(2, 1, 3)(2, 3, 1)(3, 1, 2)(3, 2, 1)   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - groupby 함수(function) /// 설명 반복 가능한 객체(iterable)로 부터 키와 그룹(list)으로 된 이터레이터를 만들어 줍니다. key 함수는 객체의 각 요소에 적용되며, 일반적으로는 해당 key 함수로 정렬을 한 후 사용합니다. ※ 형식 itertools.groupby(iterable, key=None) 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 import  itertools   test_str  =   'abcdABCeASEhhi'   test_func  =   str .lower test_sorted  =  sorted(test_str, key = test_func) print (test_sorted)   # ['a', 'A', 'A', 'b', 'B', 'c', 'C', 'd', 'e', 'E', 'h', 'h', 'i', 'S']   test_key  =  [] test_group  =  []   for  k, g  in  itertools.groupby(test_sorted, test_func):     test_key.append(k)     test_group.append(list(g))   print (test_key)   # ['a', 'b', 'c', 'd', 'e', 'h', 'i', 's'] print (test_group)  

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

파이썬(Phthon): itertools 모듈 - islice 함수(function) /// 설명 반복 가능한 객체(iterable)에서 선택적으로 요소들을 추출하여 이터레이터를 만듭니다. 음수를 사용할 수 없습니다. ※ 형식 itertools.islice(iterable, stop) itertools.islice(iterable, start, stop[, step]) 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 import  itertools   test_str  =   'abcdefghhijk'   test_is  =  itertools.islice(test_str,  6 )   for  i  in  test_is:      print (i, end = '' )   # abcdef   print ()   test_is  =  itertools.islice(test_str,  3 ,  10 )   for  i  in  test_is:      print (i, end = '' )   # defghhi   print ()   test_is  =  itertools.islice(test_str,  3 ,  10 ,  2 )   for  i  in  test_is:      print (i, end = '' )   # dfhi   print ()   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - filterfalse 함수(function) /// 설명 반복 가능한 객체의 요솟값을 적용한 predicate의 반환값이 False 인 요소들로 이터레이터를 만듭니다. ※ 형식 itertools.filterfalse(predicate, iterable) reference https://docs.python.org/3/library/itertools.html#module-itertools /// 예제 1 2 3 4 5 6 7 8 9 import  itertools   test_pd  =  lambda x: x  -   1 #               _     _     _ test_list  =  [ 2 ,  1 ,  2 ,  1 ,  3 ,  1 ]   test_ff  =  itertools.filterfalse(test_pd, test_list) print (list(test_ff))   # [1, 1, 1]   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - dropwhile 함수(function) /// 설명 반복 가능한 객체의 요솟값을 적용한 predicate의 반환값이 최초(포함)로 False 가 나온 이후의 요소들을 이터레이터로 만들어 줍니다. ※ 형식 itertools.dropwhile(predicate, iterable) reference https://docs.python.org/3/library/itertools.html#module-itertools /// 예제 1 2 3 4 5 6 7 8 import  itertools   test_pd  =  lambda x: x  <   3 test_list  =  [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ]   test_dh  =  itertools.dropwhile(test_pd, test_list) print (list(test_dh))   # [3, 4, 5, 6]   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - cycle 함수(function) /// 설명 반복 가능한 객체의 요소들을 반환합니다. 마지막 요소가 반환되면 맨 처음 요소부터 다시 시작합니다. ※ 형식 itertools.cycle(iterable) 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 import  itertools   test_str  =   'str'   test_count  =  itertools.cycle(test_str) print ( next (test_count))   # s print ( next (test_count))   # t print ( next (test_count))   # r print ( next (test_count))   # s print ( next (test_count))   # t print ( next (test_count))   # r print ( next (test_count))   # s # print(next(test_count)) # ... # ... endless     Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - compress 함수(function) /// 설명 selectors에서 True값에 해당하는 data의 요소들을 이터레이터로 만들어 줍니다. data 나 selectors중 적은 요소를 가진 인자를 우선으로 합니다. ※ 형식 itertools.compress(data, selectors) reference https://docs.python.org/3/library/itertools.html#module-itertools /// 예제 1 2 3 4 5 6 7 8 9 10 import  itertools   test_data  =   'ABCDEFGHI' # test_s = [1, 0, 1, 0, 1, 0, 1, 0] test_s  =  [ True ,  False ,  True ,  False ,  True ,  False ,  True ,  False ]   test_cp  =  itertools.compress(test_data, test_s)   print (list(test_cp))   # ['A', 'C', 'E', 'G']   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - combinations_with_replacement 함수(function) /// 설명 반복가능한 객체(iterable)에서 r 길이를 가진 서브 시퀀스들을 반환합니다.(중복조합:combination with repetition) ※ 형식 itertools.combinations_with_replacement(iterable, r) 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 import  itertools   test_1  =   'ab' test_2  =  [ 1 ,  2 ,  3 ]   test_c  =  itertools.combinations_with_replacement(test_1,  2 ) test_sorted  =  sorted(test_c)   for  i  in  test_sorted:      print (i, end = '' )   # ('a', 'b')('a', 'c')('a', 'd')('b', 'd')('c', 'b')('c', 'd')   print ()   test_c  =  itertools.combinations_with_replacement(test_2,  3 ) test_sorted  =  sorted(test_c)   for  i  in  test_sorted:      print (i, end = '' )      # (1, 2, 3)(1, 2, 4)(1, 2, 5)(1, 3, 4)(1, 3, 5)(1, 4, 5)(2, 3, 4)(2,

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

파이썬(Phthon): itertools 모듈 - combinations 함수(function) /// 설명 반복가능한 객체(iterable)에서 r 길이를 가진 서브 시퀀스들을 반환합니다.(조합:combination) ※ 형식 itertools.combinations(iterable, r) 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 import  itertools   test_1  =   'acbd' test_2  =  [ 1 ,  2 ,  3 ,  4 ,  5 ]   test_c  =  itertools.combinations(test_1,  2 ) test_sorted  =  sorted(test_c)   for  i  in  test_sorted:      print (i, end = '' )   # ('a', 'b')('a', 'c')('a', 'd')('b', 'd')('c', 'b')('c', 'd')   print ()   test_c  =  itertools.combinations(test_2,  3 ) test_sorted  =  sorted(test_c)   for  i  in  test_sorted:      print (i, end = '' )      # (1, 2, 3)(1, 2, 4)(1, 2, 5)(1, 3, 4)(1, 3, 5)(1, 4, 5)(2, 3, 4)(2, 3, 5)(2, 4, 5)(3, 4, 5) cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터

파이썬[Python]: itertools - from_iterable 메서드

파이썬(Phthon): itertools 모듈 - chain 함수 - from_iterable 메서드(method) /// 설명 chain()를 위한 대체 생성자입니다. ※ 형식 classmethod chain.from_iterable(iterable) reference https://docs.python.org/3/library/itertools.html#module-itertools /// 예제 1 2 3 4 5 6 7 8 9 10 import  itertools   test_1  =   'This' test_2  =  [ ' ' ,  'i' ,  's' ,  ' ' ,  'a' ,  ' ' ] test_3  =  [ 1 ,  2 ,  3 ,  4 ]   test  =  itertools.chain.from_iterable([test_1, test_2, test_3]) print (test)   # <itertools.chain object at 0x00000294C562BFA0> print (list(test))   # ['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 1, 2, 3, 4]   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - chain 함수(function) /// 설명 인자로 들어온 모든 반복 가능한 객체들을 하나의 이터레이터로 만들어 줍니다. ※ 형식 itertools.chain(*iterables) reference https://docs.python.org/3/library/itertools.html#module-itertools /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 import  itertools   test_1  =   'This' test_2  =  [ ' ' ,  'i' ,  's' ,  ' ' ,  'a' ,  ' ' ] test_3  =  [ 1 ,  2 ,  3 ,  4 ]   test  =  itertools.chain(test_1, test_2, test_3) print (test)   # <itertools.chain object at 0x00000294C562BFA0>   for  i  in  test:      print (i, end = '' )   # This is a 1234   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): itertools 모듈 - accumulate 함수(function) /// 설명 기본값으로 누적되어진 값을 반환합니다.(a1, a1+a2=b1, b1+a3=c1, c1+a4 ...) func 가 인자로 주어진 경우 함수에 주어진 연산을 누적으로 행하며, initial 은 초기값을 설정하는데 사용됩니다. list = [a1, a2, a3, a4, a5 ...] (a1, a1 op a2 = b, b op a3 = c, c op a4 = d, d op a5 ...) ※ 형식 itertools.accumulate(iterable[, func, *, initial=None]) 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 import  itertools   test  =  [ 1 ,  4 ,  6 ,  8 ,  2 ,  3 ,  0 ,  5 ]   test_ac  =  list(itertools.accumulate(test)) print (test_ac)   # [1, 5, 11, 19, 21, 24, 24, 29]   test_func  =  lambda x, y: x  +  y test_ac  =  list(itertools.accumulate(test, test_func)) print (test_ac)   # [1, 5, 11, 19, 21, 24, 24, 29]   test_ac  =  list(itertools.accumulate(test, initial = 10 )) print (test_ac)   # [10, 11, 15, 21, 29, 31, 34, 34, 39]   test_ac  =  list(itertools.accumulate(test, max)) print (test_a

파이썬[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( 0 ,  5 ):     test_list.append(i)   test_list.append( 1 )   for  i  in  itertools.repeat( 0 ,  5 ):     test_list.append(i)   print (test_list) # [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python

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

파이썬(Phthon): itertools 모듈 - count 함수(function) /// 설명 인자 start에서 시작하여 step 단위만큼 차이가 생기는 값들을 반환하는 이터레이터를 생성합니다. map() 이나 zip()에 자주 사용됩니다. ※ 형식 itertools.count(start=0, step=1) 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 import  itertools   test_count  =  itertools.count() print ( next (test_count))   # 0 print ( next (test_count))   # 1 print ( next (test_count))   # 2 # print(next(test_count)) # ... # ... endless   test_str  =   'This is a string.\n' str_count  =   0   for  i  in  itertools.count():      if  test_str[i]  = =   '\n' :         break      else :         str_count  + =   1   print ( 'length: ' , str_count)   # length:  17   test_f  =   0. 0 test_step  =   0. 5 for  i  in  itertools.count( 0. 0 , test_step):      if  test_f  <   5 :          print (i, end = '  ' )