0

Sorry for the puzzling title. For example, I have a list: [a, b, c, d]

I want to generate a ranked list with different combinations in this format:

[a]
[b]
[c]
[d]
[a, b]
[a, c]
[a, d]
[a, b, c]
[a, b, d]
[a, b, c, d]

I'm having trouble in generating this list. So far what I did first was generate each list by adding an element per iteration:

[]
[a]
[a, b]
[a, b, c]

Then I generated the lengths of how the ranked list should look like:

[]
[]
[]
[]
[a]
[a]
[a]
[a, b]
[a, b]
[a, b, c]

Now I'm stuck from here. Is there a library in Python that allows me to do this or can you only do this manually in code? The last thing I have to do is to do a one-to-one list appending from the original list that I generated at the top.

Here's the code of what I have attempted, assume original_list is the original list I made at the top and new_list is the list that I have generated right above this text:

for x in range(0, len(original_list)):
    new_list[x].append(original_list[x])

This doesn't work apparently since it appends every item from original_list to the first 4 items in new_list.

EDIT: The elements should be alphabetical with only the last element having different combinations with no repeating element since I'm attempting this on a list with 21 items.

0

3 Answers 3

1

Use simple iteration through list appending required to a new list:

lst = ['a', 'b', 'c', 'd', 'e']

nlst = []
for i in range(len(lst)):
    for y in lst[i:]:
        nlst.append(lst[:i] + list(y))

for x in nlst:
    print(x)

# ['a']
# ['b']
# ['c']
# ['d']
# ['e']
# ['a', 'b']
# ['a', 'c']
# ['a', 'd']
# ['a', 'e']
# ['a', 'b', 'c']
# ['a', 'b', 'd']
# ['a', 'b', 'e']
# ['a', 'b', 'c', 'd']
# ['a', 'b', 'c', 'e']
# ['a', 'b', 'c', 'd', 'e']
Sign up to request clarification or add additional context in comments.

6 Comments

I've tried this but it only works on 4 items. I'm trying to apply it in a list with 21 items.
It should work on list with any number of items. I believe you're looking only combinations that start with 'a' except for single items.
I tried adding 'e' to the list however it also produced ['a', 'b', 'd', 'e'], ['a', 'c', 'd', 'e']. The elements should be alphabetical with the last element only having different combinations.
@Austin no need to use combinations just iterate over lst[i:]
@DanielMesejo, True that! Overthinking me. Thanks.
|
1

Using the powerset recipe from itertools recipes, you could do:

from itertools import chain, combinations


def powerset(iterable):
    s = list(iterable)
    it = chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
    return map(list, (e for e in it if e))


result = sorted(powerset(['a', 'b', 'c', 'd']), key=lambda x: (len(x), ''.join(x)))
for s in result:
    print(s)

Output

['a']
['b']
['c']
['d']
['a', 'b']
['a', 'c']
['a', 'd']
['b', 'c']
['b', 'd']
['c', 'd']
['a', 'b', 'c']
['a', 'b', 'd']
['a', 'c', 'd']
['b', 'c', 'd']
['a', 'b', 'c', 'd']

UPDATE

Given the updated requirements you could do:

lst = ['a', 'b', 'c', 'd']
length = len(lst)


def patterns(l):
    for i in range(length):
        for c in l[i:]:
            yield l[:i] + [c]


for pattern in sorted(patterns(lst), key=lambda x: (len(x), ''.join(x))):
    print(pattern)

Output

['a']
['b']
['c']
['d']
['a', 'b']
['a', 'c']
['a', 'd']
['a', 'b', 'c']
['a', 'b', 'd']
['a', 'b', 'c', 'd']

4 Comments

Thanks for correct words but this the powerset of the list. I'm trying to generate something similar but it should only follow the pattern above.
So you want only the ones that start with 'a'?
Could you be more specific about the pattern above?
The elements should follow the original list with only the last element having different combinations with no repeating element in every iteration as you add every item from the original list. So If I have let's say 10 elements, it should start with the 10 single elements, 9 combinations of two with 'a' as the first element, 8 combinations of threes with 'a', 'b' as the first two elements, 7 combinations of fours with 'a', 'b', 'c' as the first 3, and so on an so forth.
0

Try this:

from itertools import combinations

a = ['a', 'b', 'c', 'd']
result = [list(combinations(a,i))for i in range(1,len(a)+1)]

and for printing it like that:

for i in result:
    print(*list(i), sep='\n')

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.