Generate list / array bringing all combinations of n elements taken p to p in python 3

0

Example: A B C D 4 elements result: WITH TWO ELEMENTS AB B.C AD BC BD CD

WITH THREE ELEMENTS ABC ABD ACD BCD

    
asked by anonymous 30.04.2017 / 01:52

1 answer

1

Here it is:

from itertools import permutations
import re

caracteres = ['A', 'B', 'C', 'D']

for i in permutations(caracteres,2): # 2 elementos
    i = re.sub(r'\W',"",str(i)) # Retirando outros caracteres que não sejam letras
    print(i)

print()
for i in permutations(caracteres,3): # 3 Elementos
    i = re.sub(r'\W',"",str(i))
    print(i)

print()
for i in permutations(caracteres,4): # 4 Elementos
    i = re.sub(r'\W',"",str(i))
    print(i)
    
30.04.2017 / 02:34