Python Forum
self learning itertools for complex structures
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
self learning itertools for complex structures
#1
hello, im a newbie, been digging around to find a solution to a problem for the creation of a complex structure using itertools - permutation combination.

So far i've been using from itertools import product, combinations, combinations_with_replacement to acheive the basic stuff. But i want to create something else and i don't know where to start. Ok, so im going to copy paste my question from stackflow:

from itertools import product, combinations, combinations_with_replacement

flavors  = ["chocolate", "vanilla", "strawberry", "coffee"]
sizes    = ["small", "medium", "large"]
toppings = ["sprinkles", "gummy bears", "oreos", "cookie dough", "snickers", "brownies"]
syrups   = ["hot fudge", "caramel", "strawberry", "white chocolate"]

#
# pick a flavor and a size
for flavor,size in product(flavors, sizes):
    #
    # pick three toppings, but no more than one of each
    for top_a, top_b, top_c in combinations(toppings, 3):
        #
        # pick three syrups, allowing repeats
        for syr_a, syr_b, syr_c in combinations_with_replacement(syrups, 3):
            #
            # now do something with the result:
            print(", ".join([flavor, size, top_a, top_b, top_c, syr_a, syr_b, syr_c]))
in this example itertools is used to print all possible outcomes,easy as 123. Been digging 16hrs a day for the past couple days, to built a much more complex structure with arguments and parameters [algorithms], and i'm struggling to find my way trough

e.g i have n number of list and i want to see all the possible outcomes, where the outcome is taken from 3 lists

list01: 01,02,03,04,05
list02: 01,03,07,10
list03: 02,03,06,08,09
list04: 01,03,05,06,07
outcome : [a,b,c], [a,d,e], [a,f]
for e.g,
[a,b,c] is taken from a list01
[a,d,e] is taken from list02, and
[a,f] is taken from list03
basically, the outcome needs to be validated, for e.g [a] is present across the 3 lists, [b,c,d,e,f] is unique in a way [if b is choosen in list01, it cannot be choosen in list02...

1 possible outcome could be [01,02,03],[03,07,10],[03,06], under list01/02/03/
what im tryin to accomplish is to run a permutation across n number of list, 3 combinations, then further create the outcome : [a,b,c], [a,d,e], [a,f]

[/quote]
Reply
#2
It looks like they may have deleted your SO question (not so welcoming, perhaps?).

I'm having a hard time understanding your question - mostly where the numbers and letters meet. Could you phrase it this way?
(1) Actual Python data structures that represent your input.
(2) Actual Python data structures that represent your output.
(3) The code you've written which attempts to bridge (1) and (2).

Each of those three should be a runnable snippet of code. For (3) it would be best to include the actual output (potentially a stacktrace) as well as (2), the desired output. Keeping them as minimal as possible to reproduce your problem will also maximize the chance of fast, high quality replies :)
Reply
#3
(May-04-2018, 12:24 PM)enrikS Wrote: to built a much more complex structure with arguments and parameters
You need to explain in details the complex structure that you want to build. The above description is very difficult to understand. Try to describe the expected output with a complete list of simple conditions that it must fulfill.
Reply
#4
Thank you so much for your replies. Yes indeed SO so not welcoming but still it's a great place to learn and as the saying goes, keep the best, ignore the rest lol.

I've learned so much these couple of days, spent some time learning and understanding the mechanism / nature behind each functions and arguments when writing that code. That said, just completed that "complex structure on my own" but still time itself is a variable that we can't pause lol. Just in love with pYthon in a very obsessive way.

So keep it up, enjoying that community.
Reply
#5
If you wouldn't mind sharing, I'm very curious about what you wrote.
Reply
#6
@micseydel

The lines themselves are variables. 2 coordinates for X and Y, then Cartesian Product of the lines with their variables.
Quote:what im tryin to accomplish is to run a permutation across n number of list, 3 combinations, then further create the outcome : [a,b,c], [a,d,e], [a,f]
Instead of writing just one code, i had to use multiple tools to accomplish these tasks. And of course ran into the issue of speed v/s disk space. My conclusion -- nearly anything is possible if you change your angle of perception to find a new approach to a problem.

Next goal is to invest in building my own pc to run my codes.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Repition structures? Katididit 3 2,020 Apr-13-2020, 06:24 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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