Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Itertools Help Please
#1
I tried reading the documentation but it's a little overwhelming.
I have a list of strings. The length of the list and the length of the strings can change in unpredictable ways.
I want to write simple function that will work every time.

Suppose I have a list like this...
['ACF', 'BGH', 'JKLM', 'QWE'] 
What I want to do is generate all possible combinations but each "Digit" is only allowed to be from its own item in the list.
Sorry, I don't know how to word what I mean.

Example:

ABJQ CBJQ FBJQ AGJQ CGJQ FGJQ AHJQ CHJQ FHJQ ABKQ CBKQ.....etc etc

I suspect itertools has a way to do this but there's too many options and I can't find it.
Thanks.
Edit: Never mind. Took about 30 minutes on YouTube but I found out itertools.product will probably work. Just need to figure out how to deal with the fact the list can vary in length now. :D
Reply
#2
from itertools import product

What you are looking for is a Cartesian product of sets (itertools.product):

sets = ['ACF', 'BGH', 'JKLM', 'QWE']
list(map(lambda x: ''.join(reversed(x)), product(*sets[::-1])))
Output:
['ABJQ', 'CBJQ', 'FBJQ', 'AGJQ', 'CGJQ', 'FGJQ', 'AHJQ', 'CHJQ', 'FHJQ', 'ABKQ', 'CBKQ', 'FBKQ', 'AGKQ', 'CGKQ', 'FGKQ', 'AHKQ', 'CHKQ', 'FHKQ', 'ABLQ', 'CBLQ', 'FBLQ', 'AGLQ', 'CGLQ', 'FGLQ', 'AHLQ', 'CHLQ', 'FHLQ', 'ABMQ', 'CBMQ', 'FBMQ', 'AGMQ', 'CGMQ', 'FGMQ', 'AHMQ', 'CHMQ', 'FHMQ', 'ABJW', 'CBJW', 'FBJW', 'AGJW', 'CGJW', 'FGJW', 'AHJW', 'CHJW', 'FHJW', 'ABKW', 'CBKW', 'FBKW', 'AGKW', 'CGKW', 'FGKW', 'AHKW', 'CHKW', 'FHKW', 'ABLW', 'CBLW', 'FBLW', 'AGLW', 'CGLW', 'FGLW', 'AHLW', 'CHLW', 'FHLW', 'ABMW', 'CBMW', 'FBMW', 'AGMW', 'CGMW', 'FGMW', 'AHMW', 'CHMW', 'FHMW', 'ABJE', 'CBJE', 'FBJE', 'AGJE', 'CGJE', 'FGJE', 'AHJE', 'CHJE', 'FHJE', 'ABKE', 'CBKE', 'FBKE', 'AGKE', 'CGKE', 'FGKE', 'AHKE', 'CHKE', 'FHKE', 'ABLE', 'CBLE', 'FBLE', 'AGLE', 'CGLE', 'FGLE', 'AHLE', 'CHLE', 'FHLE', 'ABME', 'CBME', 'FBME', 'AGME', 'CGME', 'FGME', 'AHME', 'CHME', 'FHME']
# Note. It is danger to apply list to map in this case, because this could lead to a huge array
# the shorter solution (orderless solution) would be the following:
for item in  map(''.join, product(*sets)):          
    print("Combination: ", item)
Output:
... ... Combination: ABJQ Combination: ABJW Combination: ABJE Combination: ABKQ Combination: ABKW Combination: ABKE ....
Reply
#3
(Apr-01-2018, 11:36 PM)scidam Wrote:
for item in  map(''.join, product(*sets)):          
    print("Combination: ", item)
Output:
... ... Combination: ABJQ Combination: ABJW Combination: ABJE Combination: ABKQ Combination: ABKW Combination: ABKE ....
This is exactly what I wanted, Thanks. I had thought of something similar but it was missing the * so it was not working right.
Thanks sooo much! Major headache solved.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  itertools and amazing speed Pedroski55 8 1,985 Nov-11-2022, 01:20 PM
Last Post: Gribouillis
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 2,356 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Making lists using itertools and eliminating duplicates. mike3891 2 2,190 Oct-26-2020, 05:39 PM
Last Post: bowlofred
  Python3 itertools.groupby printing the key august 1 2,046 Aug-17-2020, 05:46 AM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 1,812 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  itertools.zip_shortest() fo unequal iterators Skaperen 10 6,581 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  can itertools compact a list removing all of some value? Skaperen 6 3,102 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  Help with itertools jarrod0987 1 1,782 Jun-10-2019, 02:41 AM
Last Post: Larz60+
  ImportError: No module named jaraco.itertools in Python manhnt 0 2,940 Nov-08-2018, 11:41 AM
Last Post: manhnt
  Need help with itertools.islice() Charles1 2 2,787 Sep-19-2018, 10:32 AM
Last Post: Charles1

Forum Jump:

User Panel Messages

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