Bottom Page

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?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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']
Skaperen likes this post
Quote
#3
it's the repeat= keyword argument i didn't have when i tried itertools.product().
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)