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

Asked

Viewed 1,190 times

0

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

WITH THREE ELEMENTS ABC ABD RCA BCD

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)
  • Thank you very much Antony,

  • I made a change from permutations to Combinations and resulted in what I needed!

  • How I would make the object-character collection and cluster size required for the user to type?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.