1

I have a following list:

mylist = ['car', 'truck', 'ship']

Currently I am able to only get all the possible combinations of 2 strings using this:

from itertools import combinations
print(list(combinations(mylist,2)))

which gives me:

[('car', 'truck'), ('car', 'ship'), ('truck', 'ship')]

However, one combination is actually all the 3 strings. Thus I would like my outcome to be:

[('car', 'truck'), ('car', 'ship'), ('truck', 'ship'), ('car', 'truck', 'ship')]
3
  • I'm not sure what you're asking. Do you want all combinations except those of length 1? Commented Nov 21, 2016 at 11:45
  • why is ('car') not in your result? what is it you actually need? Commented Nov 21, 2016 at 11:46
  • 2
    If order is not important, list(set(combinations(mylist,2)).union(set(combinations(mylist, 3))] Commented Nov 21, 2016 at 11:56

4 Answers 4

5

This is an adjusted case of the powerset. Typically the code for the powerset in Python looks like this:

from itertools import chain, combinations

def powerset(it):
    yield from chain.from_iterable(combinations(it, r) for r in range(len(it)+1))

You can change it, though, to only accept results within a certain range. In your case, it's from 2 to the 3:

from itertools import chain, combinations

def adjusted_powerset(it):
    yield from chain.from_iterable(combinations(it, r) for r in range(2, 3))

See it in action here.

If you need it to become more general, play with the range parameters. A nice template would be to create a powerset helper:

from itertools import chain, combinations

def powerset_helper(it, start, stop):
    yield from chain.from_iterable(combinations(it, r) for r in range(start, stop+1))

def powerset(it):
    yield from powerset_helper(it, 0, len(it))

def adjusted_powerset(it):
    yield from powerset_helper(it, 2, 3)
Sign up to request clarification or add additional context in comments.

Comments

4

How about this,

from itertools import combinations

mylist = ['car', 'truck', 'ship']
result = list()

for r in [2, 3]:
    result.extend(combinations(mylist, r))

print(result)
[('car', 'truck'), ('car', 'ship'), ('truck', 'ship'), ('car', 'truck', 'ship')]

Comments

3

Actually, another combination is collections containing only one word, as well as the empty set. The set of all possible combinations n choose k for all * 0 <= k <= n* is called the powerset of the collection.

The documentation page of itertools gives an example of how to produce it. I have slightly modified it to accept a minlength variable (in your case minlength=2)

def powerset(iterable, minlength=0):
    "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(minlength, len(s)+1))

3 Comments

This is a good start. Make sure you add a filter to this which removes all neither-length-2-nor-3 combinations.
@erip: thanks for the note, I edited the answer. I just noticed the default case, because it is easy to forget trivial subsets (like 0 and 1 length)
That's another nice way to do it. +1
1

itertools.combinations supports list length as an input parameter.

def combinations(iterable, r):
    # combinations('ABCD', 2) --> AB AC AD BC BD CD
    # combinations(range(4), 3) --> 012 013 023 123
    pool = tuple(iterable)
    n = len(pool)
    if r > n:
        return
    indices = range(r)
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        yield tuple(pool[i] for i in indices)

For your case, simply add 2-string and 3-string together:

from itertools import combinations
print(list(combinations(mylist,2)) + list(combinations(mylist,3)))

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.