1

I have a list:

[0, 1, 2, 3, 4, 5]

I would like to combine the first item all others except the last. The result should be a list, as the list below:

[[0], [0,1], [0,2], [0,3], [0,4], [0,1,2], [0,1,3] [0,1, 4], [0,2,3], [0,2,4], [0,3,4], [0,1,2,3], [0,1,2,4], [0, 2,3,4], [0,1,2,3,4]]

How can I do this? Thank you!

2
  • Unhelpful short answer - probably something in the itertools module. Commented Aug 20, 2013 at 12:22
  • Why is [1] excluded? Commented Aug 20, 2013 at 12:27

6 Answers 6

2
import itertools

a = [0, 1, 2, 3, 4, 5]

base = (a[0],)
items = a[1:-1]
combos = [base + combo for length in range(len(items)+1) for combo in itertools.combinations(items, length)]

# In case it matters that the sublists are lists rather than tuples:
combos = [list(combo) for combo in combos]

print combos
# [[0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [0, 1, 2, 3, 4]]
Sign up to request clarification or add additional context in comments.

2 Comments

+1 but you can make it simpler by removing the inner square brackets in combos = [[base + combo ... and removing the next line. (also you can write range(len(items)+1) instead of range(0, len(items)+1))
@flornquake Good suggestions - that eliminates the chain.from_iterable step, too.
1

First compute the power set of the all but the first item. Searching on "python power set" you'll get several hits, including this one. You didn't specifically mention it, but you probably want the results in lexographical order, and the implementation I selected gets you most of the way there.

That will give you all the combinations you need, .e.g [[], 1, ..., [1,2,3,4,5]] (note this includes the empty set and the whole set itself). Now just prepend 0 to each of these gives [[0],[0,1],...[0,1,2,3,4,5]].

1 Comment

Assisting instead of giving the program +1 :)
0

This problem is related to powersets

>>> L = [0, 1, 2, 3, 4, 5]
>>> [[L[0]] + [k for j,k in enumerate(L[1:-1]) if i>>j&1] for i in range(1<<(len(L)-2))]
[[0], [0, 1], [0, 2], [0, 1, 2], [0, 3], [0, 1, 3], [0, 2, 3], [0, 1, 2, 3], [0, 4], [0, 1, 4], [0, 2, 4], [0, 1, 2, 4], [0, 3, 4], [0, 1, 3, 4], [0, 2, 3, 4], [0, 1, 2, 3, 4]]

If you want them sorted shortest to longest:

>>> M = [[L[0]] + [k for j,k in enumerate(L[1:-1]) if i>>j&1] for i in range(1<<(len(L)-2))
>>> sorted(M, key=len)
[[0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 1, 2], [0, 1, 3], [0, 2, 3], [0, 1, 4], [0, 2, 4], [0, 3, 4], [0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [0, 1, 2, 3, 4]]

Comments

0

Its a combination of the first element plus the powerset of all the elements minus the first and last elements:

from itertools import chain, combinations

test = [0, 1, 2, 3, 4, 5]

def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))


print [test[:1] + list(c) for c in powerset(test[1:-1])]

# [[0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [0, 1, 2, 3, 4]]   

Comments

0

Here is a generator.

from itertools import combinations

def custom_combination_gen(l):
    start = [l[0]]
    L = l[1:-1]
    yield start
    for y in range(1, len(L)+1):
        for x in combinations(L, y):
            yield start + list(x)

Running the code:

print list(custom_combination_gen([0,1,2,3,4,5]))
[[0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [0, 1, 2, 3, 4]]

Comments

0
import itertools
a = [0, 1, 2, 3, 4, 5]
myList = []
myFinalList = []
for i in xrange(0,len(a)-2): myList += list(itertools.combinations(a[1:-1],i))
for item in myList: myFinalList.append(list(item)+[a[0]])
print myFinalList

1 Comment

Hi, your post has been flagged as "low quality", probably because it consists solely of code. You could massively improve your answer by providing an explanation of exactly how and why this answers the question?

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.