0

I'm facing rather simple but for me quite problematic thing cause I can't get through this. So far I've been able to identify certain tools in Python (namely chain, combinations, permutations and product from itertools). The problem is that none of this helped me much or it might be that there is a way to use them properly to solve the problem.

The problem is:

Let's assume I'm having a list of sets (or a list of lists):

[(1,), (2,), (3,), (1,2), (2,3)]

What I want to achieve is to get certain combination of these sets but the result can't combine the inner body of every set, i. e. it can't produce something like this:

[(1,2,3), (2,3,1)]

cause the sets has to be separated. So the proper result would be something like this:

[[(1,),(2,),(1,2)], [(1,), (2,3)]]

So as you can see it would produce a sets of lists in a list cause the whole idea is to save the sets along the combination. It would be nice to provide max length lists containing sets, i. e. all combinations with length from 1 to 3.

Itertools is nice but it does ruin the sets making them undistinguishable. My idea was to maybe make combination of list of indexes where every index will represent a set, i. e.

[0,1,2,3,4] -> [(0,2,3), (0,1)] -> which translates into -> [[(1,),(3,),(1,2)], [(1,), (2,)]]

I hope you might get an idea what I want to get here.

4
  • I don't understand your description of the problem. Which "certain combination of these sets"? What do you mean by "the sets has to be separated"? Commented Jun 3, 2020 at 13:45
  • If you want to combine list of lists (or list of tuples), then you can do it manually afterwards. Commented Jun 3, 2020 at 13:47
  • @Błotosmętek - let's say that every set in a list has to be "untouchable". Itertools usually treats a set of [1,2,3] as separate elements in the end making it impossible to find from what sets the the list element is made. Example: [1,2,3] and [4,5] in the combination has to be represented as ([1,2,3], [4,5]) - single element of two lists, whereas combination and permutation in Python treats it as (1,2,3,4,5) - which does not represent the separate sets. I hope it might clear a little bit what I want to achieve. Commented Jun 3, 2020 at 14:10
  • 1
    So in fact you want all combinations of your sets (they're actually tuples, but that doesn't matter)? Commented Jun 3, 2020 at 14:14

1 Answer 1

2

If I understand your question correctly, you want all permutations of the list items, without combining the individual list items. And that for permutations of lengths 1 to "length of outer list".

In that case, the following may work:

import itertools as it

a = [(1,), (2,), (3,), (1,2), (2,3)]

perms = it.chain.from_iterable(it.permutations(a, n) for n in range(len(a)))

perms will be an iterable (so convert it to list if needed), and will yield 206 elements, each an iterable of "length" 1, 2, 3, 4 or 5 (= len(a), the maximum). Again, if you want those elements to be a list instead, also convert them (beforehand, or at the point where needed).

If you want something like the result in your example (list of list of tuples), use e.g.:

list(it.chain([list(p) for p in it.permutations(a, n)] for n in range(len(a))))
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, pretty much it does the job - I only had to "unpack" the list so it wouldn't be multidimensional cause originally if I wanted to get to the permutations with for example exactly two sets I had to go like "list(a)[2][n]", where n is any number in range. Simply using "chain.from_iterable(a)" solved the issue. But it might just be my mistake that it came multidimensional like that. So thank you very much for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.