파이썬 리스트의 모든 조합 구하기

Python

Posted by kwon on 2019-08-20

리스트의 모든 조합 구하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#중복 허용
items = ['1', '2', '3', '4', '5']
from itertools import permutations
list(permutations(items, 2))

[('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '1'), ('2', '3'), ('2', '4'),
('2', '5'), ('3', '1'), ('3', '2'), ('3', '4'), ('3', '5'), ('4', '1'), ('4', '2'),
('4', '3'), ('4', '5'), ('5', '1'), ('5', '2'), ('5', '3'), ('5', '4')]

#중복 x
from itertools import combinations
list(combinations(items, 2))

[('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '3'), ('2', '4'),
('2', '5'), ('3', '4'), ('3', '5'), ('4', '5')]