naivepredictor
naivepredictor

Reputation: 898

All combinations of list elements in certain order

I have a list as follows:

((0,n1,n2,...,nX),(0,n1,n2,...,nY),(1,n1,n2,...,nZ),(2,n1,n2,...,nR),(2,n1,n2,...,nS))

I would like to return all possible combinations of list elements in such way:

(0,n1,n2,...,nX),(1,n1,n2,...,nZ),(2,n1,n2,...,nR)
(0,n1,n2,...,nY),(1,n1,n2,...,nZ),(2,n1,n2,...,nR)
(0,n1,n2,...,nX),(1,n1,n2,...,nZ),(2,n1,n2,...,nS)
(0,n1,n2,...,nY),(1,n1,n2,...,nZ),(2,n1,n2,...,nS)

So, I have understood and worked out that I need to iterate through elements and check first item of the list element to group elements by first item.

It could let me to do maybe a for loop? and try to manualy create all combinations?

However I wonder if there is any better approach?

I need to keep in mind that the elements must be in order ascending by first item of the elements --> 0, 1, 2

EDIT:

This is my list in other words:

((0,A), (0,B), (1,C), (2,D),(2,E))

how to return as follows:

(0,A),(1,C),(2,D)
(0,B),(1,C),(2,D)
(0,A),(1,C),(2,E)
(0,B),(1,C),(2,E)

?

Upvotes: 0

Views: 1730

Answers (1)

Thanassis
Thanassis

Reputation: 604

The problem becomes easier if you change your data structures a bit. More specifically, just group all elements with the same "ID" in the same list.

For your example you have 3 lists:

a = [(0,n1,n2,...,nX),(0,n1,n2,...,nY)]
b = [(1,n1,n2,...,nZ)]
c = [(2,n1,n2,...,nR),(2,n1,n2,...,nS)]

Let me know if you have trouble separating the lists out like this, and I'll amend my answer.

Then you can use the itertools.product function to get all the combinations that you want.

import itertools
for i in itertools.product(a, b, c):
    print i

Or if you want to see all the combinations as a list you can simply do:

list(itertools.product(a, b, c)) 

Similarly, you can use tuple() or set() if you want to see all the combinations as a tuple or a set.

EDIT: If you to not have your elements already grouped together, and instead you have a flattened list(or tuple) of tuples you can create a list that groups tuples according to their "ID" (i.e., the first value of the simple tuples). Here's a function to do it. I assume there is no order in how the tuples are initially given (otherwise we can probably make this grouping more efficient)

def groupList(flatlist):
    tempdict = {}
    for element in flatlist:
        id = element[0]
        if id in tempdict:
            tempdict[id].append(element)
        else:
            tempdict[id] = [element]
    return list(tempdict.values())

Now you can used this "grouped" list to get all the combinations. Let's assume that you initial list is l, then you can do:

list(itertools.product(*groupList(l)))

Notice the * when passing the argument. This tells python to use the elements of this list as separate arguments to the function.

Example Input:

l = ((0, 10), (0, 20), (1, 30), (2, 40), (2, 50))

Example Output:

[((0, 10), (1, 30), (2, 40)), ((0, 10), (1, 30), (2, 50)), ((0, 20), (1, 30), (2, 40)), ((0, 20), (1, 30), (2, 50))]

Upvotes: 1

Related Questions