1

I have N ordered slots. Each slot has a value within V possible values

N = 4 # number of objects (e.g. slots)
possible_values = ['A','B']
V = len(possible_values )

How would it be possible to generate the list of all possible combinations in Python? For instance, when V=2 and N=4, I would like to get the following list of the 2**4 different combinations:

combinations = [
    [ 'A', 'A', 'A', 'A' ], # combination 0
    [ 'A', 'A', 'A', 'B' ], # combination 1
    [ 'A', 'A', 'B', 'A' ], # combination 2
    [ 'A', 'A', 'B', 'B' ], # combination 3
    [ 'A', 'B', 'A', 'A' ], # combination 4
    [ 'A', 'B', 'A', 'B' ], # combination 5
    [ 'A', 'B', 'B', 'A' ], # combination 6
    [ 'A', 'B', 'B', 'B' ], # combination 7
    [ 'B', 'A', 'A', 'A' ], # combination 8
    [ 'B', 'A', 'A', 'B' ], # combination 9
    [ 'B', 'A', 'B', 'A' ], # combination 10
    [ 'B', 'A', 'B', 'B' ], # combination 11
    [ 'B', 'B', 'A', 'A' ], # combination 12
    [ 'B', 'B', 'A', 'B' ], # combination 13
    [ 'B', 'B', 'B', 'A' ], # combination 14
    [ 'B', 'B', 'B', 'B' ], # combination 15
]

I would like the code to work when N and V vary. For example, when N=9 slots and V=4 possible values, I expect the list of the 4**9=262144 possible combinations.

user3483203
  • 50,081
  • 9
  • 65
  • 94
Xavier M
  • 547
  • 3
  • 6
  • 13
  • 2
    You've tagged this with `numpy` and `arrays`, but you're using lists of lists. Which answer do you want? – abarnert Jul 05 '18 at 18:52
  • 4
    `help(itertools.product)` – JacobIRR Jul 05 '18 at 18:53
  • 1
    If you want to work on arbitrary Python iterables, look in [`itertools`](https://docs.python.org/3/library/itertools.html). If you want to work on NumPy arrays, look in [`scipy`](https://docs.scipy.org/doc/scipy/reference/misc.html). Either way, it should take you almost no time to find the answer. – abarnert Jul 05 '18 at 18:53
  • thanks. I fixed it by removing arrays – Xavier M Jul 05 '18 at 18:54
  • 1
    But you left NumPy, so it's still just as unclear. Do you want a general-iterable solution, or a NumPy-array solution? – abarnert Jul 05 '18 at 18:55
  • @a625993 No, that one is about getting all combinations of 0-n members of a list of n values, which is a different problem. – abarnert Jul 05 '18 at 18:56
  • 1
    what does this have to do with reinforcement learning? – Mohammad Athar Jul 05 '18 at 18:56
  • @PauloScardine Almost. But the OP is looking for the cartesian power—that is, the cartesian product of `x` with itself `n` times, not `x` with some other `y`. Also, I'm not sure the OP _knows_ he's looking for the product, so I'm not sure that answer explains enough? – abarnert Jul 05 '18 at 18:59
  • @PauloScardine Also, the OP just removed the numpy tag, so he presumably wants an `itertools.product` answer (whether he knows that or not). – abarnert Jul 05 '18 at 19:00
  • @abarnert seriously, you are a machine. :-) – Paulo Scardine Jul 05 '18 at 19:01
  • Maybe we just need multiple dups? [This](https://stackoverflow.com/questions/31961442/) shows the right answer, [this](https://stackoverflow.com/questions/329886/) explains `product` in terms of nested loops, … maybe a few more? – abarnert Jul 05 '18 at 19:01
  • 1
    https://stackoverflow.com/questions/27757973/generating-all-possibly-length-n-combinations-of-two-items-in-python How is this one? – user3483203 Jul 05 '18 at 19:04
  • Yes, it is clearly a dupe, lets mark it and move on. – Paulo Scardine Jul 05 '18 at 19:04
  • @user3483203 Yeah, that one's good. Maybe the other 3 aren't needed anymore, but… probably better to have too much information than too little? – abarnert Jul 05 '18 at 19:05

1 Answers1

0
N = 4 # number of objects (e.g. slots)
possible_values = ['A','B']

result = itertools.product(possible_values, repeat=N)

print(list(result))
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • 3
    Not sure why this is getting voted down, it provides an answer to the question. Compensating! I would suggest if you are voting down to leave feedback, it's hard to improve otherwise. – AChampion Jul 05 '18 at 19:01
  • @AChampion The question is really bad, but I believe people should vote down the bad question, not the answer. Oh well. – nosklo Jul 05 '18 at 19:03
  • 3
    One possible reason for downvotes is that it’s bare code with no explanation or discussion. – Tom Zych Jul 05 '18 at 19:04
  • There isn't much to explain though - the problem can be solved with a single call to a function defined in standard library to do exactly this. – nosklo Jul 05 '18 at 19:04
  • @nosklo agreed, but even the question has up votes! – AChampion Jul 05 '18 at 19:05
  • 3
    Not the downvote here, but I *have* downvoted you several times before for answering obvious duplicates. That could also be the case here. – user3483203 Jul 05 '18 at 19:06
  • @AChampion I guess people prefer closing questions and downvoting, than just answering to help – nosklo Jul 05 '18 at 19:06
  • 1
    You *should* close questions before you answer them *if they are duplicates* – user3483203 Jul 05 '18 at 19:06
  • 4
    Dupe-closing is helping. – user2357112 Jul 05 '18 at 19:06
  • I don't usually downvote people for answering obvious duplicates, but… yeah, the time you spent writing this (and the even more time it would take coming up with an explanation to make it worth upvoting) could be better spent looking for the dup. – abarnert Jul 05 '18 at 19:07
  • @nosklo Nobody is punishing the OP here. The question has +1 votes and multiple helpful dup links. And those dups explain the problem better than a code-only answer does. – abarnert Jul 05 '18 at 19:07
  • 1
    I don't think the downvote button should be used to say "you should close the question instead" - it was not made for that. The answer doesn't hurt - even if you close the question it can help - helping is the actual purpose of the site! – nosklo Jul 05 '18 at 19:07
  • When you hover over the downvote, it says "This answer is not useful". I believe that answers to duplicates are not useful because those questions already have answers. If your answer is better, then go answer the duplicate. – user3483203 Jul 05 '18 at 19:08
  • 1
    How can it not be useful? I am sure OP finds it very useful @user3483203 – nosklo Jul 05 '18 at 19:08
  • Everyone has their own opinions on how downvotes should be used, and the official SO policy is that all of those opinions (except people using them for trolling or retribution or stupid things like that) are reasonable. That means that if you disagree with most of the community on this, and don't want to adapt to their standards, you're going to get downvotes. But so what? It's not like your rep is so low you're in danger of getting banned or something; at worst it means you'll get to 200K a couple weeks later… – abarnert Jul 05 '18 at 19:10
  • yeah, @abarnert I will keep answering questions because I like to answer questions. People that like to close and downvote be my guest... – nosklo Jul 05 '18 at 19:11
  • @nosklo But you're still misrepresenting things. People _aren't_ closing and downvoting the question—they're closing the question as a dup and _upvoting_ it. If you really think that's unhelpful, or unfair, or whatever, I don't know what to say to that… – abarnert Jul 05 '18 at 19:12
  • 1
    @abarnert I don't! I believe dups should be closed. I just don't think answering them should be frowned upon and downvoted. – nosklo Jul 05 '18 at 19:13