python ワンライナー 重複無し組み合わせ

#重複無し
bash-3.2$ python3 -c 'from itertools import combinations ; base = list(combinations("abcde",2))  ; print(base)'
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('a', 'e'), ('b', 'c'), ('b', 'd'), ('b', 'e'), ('c', 'd'), ('c', 'e'), ('d', 'e')]
bash-3.2$ 
bash-3.2$ python3 -c 'import itertools ; seq = ("evi1","evi2","evi3","evi4") ;date = list(itertools.combinations(seq,3)) ; print(date)'
[('evi1', 'evi2', 'evi3'), ('evi1', 'evi2', 'evi4'), ('evi1', 'evi3', 'evi4'), ('evi2', 'evi3', 'evi4')]
bash-3.2$ 
#直積
>>> A = ('a', 'b', 'c')
>>> B = ('d', 'e')
>>> C = ('f')

>>> list(itertools.product(A, B))
[('a', 'd'), ('a', 'e'), ('b', 'd'), ('b', 'e'), ('c', 'd'), ('c', 'e')]

>>> list(itertools.product(A, B, C))
[('a', 'd', 'f'),
('a', 'e', 'f'),
('b', 'd', 'f'),
('b', 'e', 'f'),
('c', 'd', 'f'),
('c', 'e', 'f')]
>>> list(itertools.combinations_with_replacement(A, 3))
[('a', 'a', 'a'),
('a', 'a', 'b'),
('a', 'a', 'c'),
('a', 'b', 'b'),
('a', 'b', 'c'),
('a', 'c', 'c'),
('b', 'b', 'b'),
('b', 'b', 'c'),
('b', 'c', 'c'),
('c', 'c', 'c')]​

いいなと思ったら応援しよう!