[라이브러리] itertools / Python

2022. 4. 5. 18:16·python/라이브러리
728x90
반응형

itertools 란 효율적인 루핑을 위한 이터레이터를 만드는 함수이다.

itertools에는 아래와 같은 조합형 이터레이터가 있다.

  • combinations()
  • combinations_with_replacement()
  • product()
  • permutations()

combinations(iterable, r) : iterable에서 원소 개수가 r개인 조합 뽑기

from itertools import combinations

l = [1,2,3]

for i in combinations(l,2):
	print(i)

'''
출력 결과:
(1, 2)
(1, 3)
(2, 3)
'''

combinations_with_replacement(iterable,r) : iterable에서 원소 개수가 r개인 중복 조합 뽑기

from itertools import combinations_with_replacement

l = ['A', 'B', 'C']

for i in combinations_with_replacement(l,2):
	print(i)

'''
출력결과:
('A', 'A')
('A', 'B')
('A', 'C')
('B', 'B')
('B', 'C')
('C', 'C')
'''

permutations(iterable,r=None) : iterable에서 원소 개수가 r개인 순열 뽑기

from itertools import permutations

l = ['A', 'B', 'C']

for i in permutations(l):
	print(i)

'''
출력결과:
('A', 'B', 'C')
('A', 'C', 'B')
('B', 'A', 'C')
('B', 'C', 'A')
('C', 'A', 'B')
('C', 'B', 'A')
'''

for i in permutations() 구문에서 r을 지정하지 않거나 r=None으로 하면 만들 수 있는 최대 길이의 순열이 리턴된다.

 

product(*iterables, repeat=1) : 여러 iterable의 데카르트곱 리턴

from itertools import product

l1 = ['A', 'B']
l2 = ['1', '2']

for i in product(l1,l2,repeat=1): #l1과 l2의 모든 쌍을 지어 리턴한다
	print(i)

'''
출력결과:
('A', '1')
('A', '2')
('B', '1')
('B', '2')
'''

for i in product(l1,repeat=3): #product(l1,l1,l1,repeat=1)과 동일한 출력
	print(i)

'''
출력결과:
('A', 'A', 'A')
('A', 'A', 'B')
('A', 'B', 'A')
('A', 'B', 'B')
('B', 'A', 'A')
('B', 'A', 'B')
('B', 'B', 'A')
('B', 'B', 'B')
'''

 

reference

https://seu11ee.tistory.com/5

 

[파이썬] itertools 라이브러리 사용법 (순열, 조합)(permutations, combinations) - Python 문법

오늘은 파이썬 라이브러리 중 하나인 itertools에 대해 알아보겠습니다~! 알고리즘을 풀면 조합과 순열 개념이 자주 등장하는데요, itertools 라이브러리를 이용하면 조합과 순열을 쉽게 구할 수 있

seu11ee.tistory.com

https://yganalyst.github.io/etc/memo_18_itertools/

 

[Python] itertools, 원소의 경우의 수(순열, 조합) 추출하기

itertools 라이브러리를 활용해서 원소들의 경우의 수를 추출하는 방법을 배워보자.

yganalyst.github.io

 

728x90
반응형

'python > 라이브러리' 카테고리의 다른 글

[Python] Haversine - 두 지점의 위도 경도로 직선 거리 구하기  (0) 2022.10.11
[Python] tqdm 라이브러리 - 작업 진행률 표시  (0) 2022.07.27
[기초] Altair / 데이터 시각화  (0) 2022.07.01
[기초] Seaborn 라이브러리 정리 / Python 파이썬  (0) 2022.05.16
[기초] Pandas 라이브러리 정리 (1) / Python 파이썬  (0) 2022.05.16
'python/라이브러리' 카테고리의 다른 글
  • [Python] tqdm 라이브러리 - 작업 진행률 표시
  • [기초] Altair / 데이터 시각화
  • [기초] Seaborn 라이브러리 정리 / Python 파이썬
  • [기초] Pandas 라이브러리 정리 (1) / Python 파이썬
sillon
sillon
꾸준해지려고 합니다..
    반응형
  • sillon
    sillon coding
    sillon
  • 전체
    오늘
    어제
    • menu (614)
      • notice (2)
      • python (68)
        • 자료구조 & 알고리즘 (23)
        • 라이브러리 (19)
        • 기초 (8)
        • 자동화 (14)
        • 보안 (1)
      • coding test - python (301)
        • Programmers (166)
        • 백준 (76)
        • Code Tree (22)
        • 기본기 문제 (37)
      • coding test - C++ (5)
        • Programmers (4)
        • 백준 (1)
        • 기본기문제 (0)
      • 공부정리 (5)
        • 신호처리 시스템 (0)
        • Deep learnig & Machine lear.. (41)
        • Data Science (18)
        • Computer Vision (17)
        • NLP (40)
        • Dacon (2)
        • 모두를 위한 딥러닝 (강의 정리) (4)
        • 모두의 딥러닝 (교재 정리) (9)
        • 통계 (2)
      • HCI (23)
        • Haptics (7)
        • Graphics (11)
        • Arduino (4)
      • Project (21)
        • Web Project (1)
        • App Project (1)
        • Paper Project (1)
        • 캡스톤디자인2 (17)
        • etc (1)
      • OS (10)
        • Ubuntu (9)
        • Rasberry pi (1)
      • App & Web (9)
        • Android (7)
        • javascript (2)
      • C++ (5)
        • 기초 (5)
      • Cloud & SERVER (8)
        • Git (2)
        • Docker (1)
        • DB (4)
      • Paper (7)
        • NLP Paper review (6)
      • 데이터 분석 (0)
        • GIS (0)
      • daily (2)
        • 대학원 준비 (0)
      • 영어공부 (6)
        • job interview (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    소수
    백준
    programmers
    Python
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
sillon
[라이브러리] itertools / Python
상단으로

티스토리툴바