django-d
django-d

Reputation: 2280

Generate all possible sequential strings from list using Python 3

I have a list:

["toaster", "oven", "door"]  

I need to get ALL the possible sequential words that can be created. The output should look like this:

["toaster", "toaster oven", "toaster oven door", "oven", "oven door", "door"]

What is the most efficient way to get this list? I've looked at itertools.combinations() and a few other suggestions found on Stack Overflow, but nothing that would produce this exact result.

For example, the above list is not a powerset, because only words adjacent to each other in the input list should be used. A powerset would combine toaster and door into toaster door, but those two words are not adjacent.

Upvotes: 2

Views: 695

Answers (3)

Michal
Michal

Reputation: 1

import itertools

a = ['toaster', 'over', 'door']

result = []
for i in [itertools.combinations(a, x + 1) for x in range(len(a))]:
    result += [' '.join(e) for e in list(i)]

print(result)

What do you think about this solution? The result is:

['toaster', 'over', 'door', 'toaster over', 'toaster door', 'over door', 'toaster over door']

Upvotes: 0

Thierry Lathuille
Thierry Lathuille

Reputation: 24234

You can do it like this:

words = ["toaster", "oven", "door"]  

length = len(words)
out = []
for start in range(length):
    for end in range (start+1, length+1):
        out.append(' '.join(words[start:end]))

print(out)

# ['toaster', 'toaster oven', 'toaster oven door', 'oven', 'oven door', 'door']

You just need to determine the first and last word to use.

You could also use a list comprehension:

[' '.join(words[start:end]) for start in range(length) for end in range(start+1, length+1)]

#['toaster', 'toaster oven', 'toaster oven door', 'oven', 'oven door', 'door']

Upvotes: 10

Martijn Pieters
Martijn Pieters

Reputation: 1121366

You want to create sliding windows of increasing length, use the window() function from the top answer there inside a range() loop to increment the lengths:

from itertools import islice, chain

# window definition from https://stackoverflow.com/a/6822773

def increasing_slices(seq):
    seq = list(seq)
    return chain.from_iterable(window(seq, n=i) for i in range(1, len(seq) + 1))

for combo in increasing_slices(["toaster", "oven", "door"]):
    print(' '.join(combo))

This outputs:

toaster
oven
door
toaster oven
oven door
toaster oven door

Upvotes: 3

Related Questions