Bottom Page

Thread Rating:
• 1 Vote(s) - 2 Average
• 1
• 2
• 3
• 4
• 5
 which itertools method Skaperen Black Knight Posts: 3,323 Threads: 1,065 Joined: Sep 2016 Reputation: 13 Likes received: 103 #1 May-10-2020, 10:29 PM 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. Yoriz Posts: 1,217 Threads: 28 Joined: Sep 2016 Reputation: 118 Likes received: 275 #2 May-10-2020, 10:45 PM (This post was last modified: May-10-2020, 10:50 PM by Yoriz. Edited 1 time in total.) ```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 Skaperen Black Knight Posts: 3,323 Threads: 1,065 Joined: Sep 2016 Reputation: 13 Likes received: 103 #3 May-13-2020, 12:13 AM 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. « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)