6

I've got a list ['a', 'b', 'c', 'd'] and I need a list ['a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd'].

I've been looking at itertools, but I'm not seeing how to make this work.

For all combinations, the code would be:

from itertools import permutations
stuff = ['a','b','c','d']
for i in range(0, len(stuff)+1):
    for subset in permutations(stuff, i):
           print(subset)

What would I need to do to return only sequential combinations? I guess I could check the order for each permutation as I go, but that doesn't seem to be the best way.

5
  • Do you need 'abc' and 'd' there as well? Because otherwise I don't see any logic in your list. Commented Oct 2, 2015 at 2:55
  • Should 'abc' also be in the list you want to construct? Commented Oct 2, 2015 at 2:55
  • 2
    something like print([''.join(stuff[i:j]) for i in range(len(stuff)) for j in range(i+1, len(stuff)+1)]) Commented Oct 2, 2015 at 2:56
  • 1
    you want combinations, not permutations, or rather "ordered" combinations. See itertools.combinations Commented Oct 2, 2015 at 2:58
  • I lied, combinations are like [('a', 'b', 'c'), ('a', 'b', 'd'), ('a', 'c', 'd'), ('b', 'c', 'd')] Commented Oct 2, 2015 at 3:04

5 Answers 5

5

Quite simply:

stuff = ['a','b','c','d']
print([''.join(stuff[i:j]) for i in range(len(stuff)) for j in range(i+1, len(stuff)+1)])

Gives

['a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd']
Sign up to request clarification or add additional context in comments.

3 Comments

The stuff in the list will be more than one character... Will that cause a problem?
@Joseph I don't understand? the number of character in individual item does not matter.
sorry... went brain dead for a sec... saw len() and thought len(characters)
1

You could do this with a list in-comprehension:

>>> [''.join(['a', 'b', 'c', 'd'])[i:j+1] for i in range(4) for j in range(i, 4)]
['a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd']

Not sure if you want to do it this way though.

Comments

1

I think this should do the trick:

items = ['a', 'b', 'c', 'd']
combinations = []
for i, x in enumerate(items):
    combinations.append(x)
    accum = x
    for y in items[i+1:]:
        accum += y
        combinations.append(accum)

Comments

1

This function does it:

def subsequences(lst):
    return [''.join(lst[i: j+1])
            for i in range(len(lst)) 
            for j in range(i, len(lst))]

>>> subsequences(['a', 'b', 'c'])
['a', 'ab', 'abc', 'b', 'bc', 'c']
>>> subsequences(['a', 'b', 'c', 'd'])
['a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd']

Comments

1

Yet another possible solution (without using itertools), this time using a helper procedure for clarity:

def combine(lst):
    return [''.join(lst[0:i+1]) for i in xrange(len(lst))]

lst = ['a', 'b', 'c', 'd']
sum([combine(lst[i:]) for i in xrange(len(lst))], [])
=> ['a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd']

Comments

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.