Python Forum
which itertools method - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: which itertools method (/thread-26711.html)



which itertools method - Skaperen - May-10-2020

i have been looking for a method that can generate (an iterator) all possible combinations of a given sequence in any order. for example for AB i want to get AA AB BA and BB. for ABC i want to get AAA AAB AAC ABA ABB ABC ACA ACB ACC BAA BAB BAC BBA BBB BBC BCA BCB BCC CAA CAB CAC CBA CBB CBC CCA CCB CCC. which function or method can do that?


RE: which itertools method - Yoriz - May-10-2020

import itertools

result = (itertools.product('AB', repeat=2))
result = map(''.join, result)
print(list(result))

result = (itertools.product('ABC', repeat=3))
result = map(''.join, result)
print(list(result))
Output:
['AA', 'AB', 'BA', 'BB'] ['AAA', 'AAB', 'AAC', 'ABA', 'ABB', 'ABC', 'ACA', 'ACB', 'ACC', 'BAA', 'BAB', 'BAC', 'BBA', 'BBB', 'BBC', 'BCA', 'BCB', 'BCC', 'CAA', 'CAB', 'CAC', 'CBA', 'CBB', 'CBC', 'CCA', 'CCB', 'CCC']



RE: which itertools method - Skaperen - May-13-2020

it's the repeat= keyword argument i didn't have when i tried itertools.product().