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

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

WITH THREE ELEMENTS ABC ABD ACD BCD

Author: Armando A. Mendes, 2017-04-29

1 answers

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)
 1
Author: Antony Gabriel, 2017-04-30 00:34:22