Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which itertools method
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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']
Reply
#3
it's the repeat= keyword argument i didn't have when i tried itertools.product().
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020