python 메모

[python] itertools

2021. 5. 6. 18:28
목차
  1. 1) itertools.product()
  2. 2) itertools.permutations()
  3. 3) itertools.combinations()
  4. 4) itertools.combinations_with_replacement(): 중복조합

1) itertools.product()

itertools.product()는 두개 이상의 리스트(or 집합) 끼리의 데카르트 곱(cartesian product)를 계산하여 iterator로 반환해준다. Cartesian Product는 아래와 같이 정의된다.

A×B={(x,y)| x∈A and y∈B}

import itertools

A = [1,2,3]
list(itertools.product(A, repeat=2))
>>>> [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]

print(itertools.product(A, repeat=2))
>>>> <itertools.product object at 0x7fa24fddc7e0>

A = [[1,2,3], [4, 5]]
list(itertools.product(*A))
>>>> [(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)]

A = [1,2,3]
B = [4,5]
print(itertools.product(A, B))
>>>> <itertools.product object at 0x7fa24fddc7e0>

 

2) itertools.permutations()

itertools.permutations(iterable, r)은 iterable 객체 원소들의 길의 r 짜리 permutation을 계산하여 iterator로 반환해준다.

nPr=n!(n−r)!

import itertools

A = [1,2,3]
list(itertools.permutations(A))
>>>> [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

print(itertools.product(A, repeat=2))
>>>> <itertools.permutations object at 0x7fa24fb391a8>

B = 'abc'
list(itertools.permutations(B))
>>>> [('a', 'b', 'c'),
      ('a', 'c', 'b'),
      ('b', 'a', 'c'),
      ('b', 'c', 'a'),
      ('c', 'a', 'b'),
      ('c', 'b', 'a')]

list(itertools.permutations(B, 2))
>>>> [('a', 'b'),
      ('a', 'c'),
      ('b', 'a'),
      ('b', 'c'), 
      ('c', 'a'),
      ('c', 'b')]

 

3) itertools.combinations()

itertools.combinations(iterable, r)은 iterable 객체 원소들의 길의 r 짜리 combination들을 계산하여 iterator로 반환해준다.(r은 필수 keyword)

nCr=n!r!(n−r)!

import itertools

A = [1,2,3]
list(itertools.combinations(A, 2))
>>>> [(1, 2), (1, 3), (2, 3)]

B = 'abcde'
list(itertools.combinations(B, 3))
>>>> [('a', 'b', 'c'),
      ('a', 'b', 'd'),
      ('a', 'b', 'e'),
      ('a', 'c', 'd'),
      ('a', 'c', 'e'),
      ('a', 'd', 'e'),
      ('b', 'c', 'd'),
      ('b', 'c', 'e'),
      ('b', 'd', 'e'),
      ('c', 'd', 'e')]

 

4) itertools.combinations_with_replacement(): 중복조합

itertools.combinations_with_replacement(iterable, r)은 iterable 객체 원소들의 길의 r 짜리 combination들을 계산하는데, 중복을 허용하여 뽑는다. 즉 itertools.combinations()와는 다르게 자기자신이 다시 뽑히는 것도 허용한다.

nHr=n+r−1Cr

import itertools

A = [1,2,3]
list(itertools.combinations(A, 2))
>>>> [(1, 2), (1, 3), (2, 3)]

list(itertools.combinations_with_replacement(A, 2))
>>>> [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]

 

728x90
저작자표시 비영리 동일조건 (새창열림)

'python 메모' 카테고리의 다른 글

[python] regex 메모  (0) 2021.05.07
[python] 봐두면 유용할 수도 있는 문자열 built-in functions  (0) 2021.05.06
[python] any(), all()  (0) 2021.05.06
[pandas] DataFrame을 string으로 출력하기  (0) 2021.04.15
[pandas] 항목별 개수(value_counts) 그래프 생성  (0) 2021.04.15
  1. 1) itertools.product()
  2. 2) itertools.permutations()
  3. 3) itertools.combinations()
  4. 4) itertools.combinations_with_replacement(): 중복조합
'python 메모' 카테고리의 다른 글
  • [python] regex 메모
  • [python] 봐두면 유용할 수도 있는 문자열 built-in functions
  • [python] any(), all()
  • [pandas] DataFrame을 string으로 출력하기
Fine애플
Fine애플
이것저것
끄적끄적이것저것
Fine애플
끄적끄적
Fine애플
전체
오늘
어제
  • 분류 전체보기 (167)
    • 논문 및 개념 정리 (27)
    • Pattern Recognition (8)
    • 개발 (57)
    • python 메모 (45)
    • pytorch, tensorflow (5)
    • 알고리즘 (9)
    • Toy Projects (4)
    • 통계이론 (2)
    • Reinforcement Learning (10)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • 개발환경
  • Probability
  • 딥러닝
  • reinforcement learning
  • BigBird
  • GPU
  • Docker
  • tensorflow
  • 자연어
  • pandas
  • python
  • Bert
  • container
  • transformer
  • nlp
  • 알고리즘
  • 언어모델
  • miniconda
  • ubuntu
  • PyTorch

최근 댓글

최근 글

hELLO · Designed By 정상우.
Fine애플
[python] itertools
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.