views:

47

answers:

3

What is the best way performance wise to take a list of words and turn them into phrases in python.

words = ["hey","there","stack","overflow"]
print magicFunction(words)
>>> ["hey","there","stack","overflow", "hey there stack","hey there", "there stack overflow","there stack", "stack overflow", "hey there stack overflow" ]

Order doesnt matter....

UPDATE: Should have been more specific, the words have to be consecutive, as in the list as in my example print out. So we could have "hey there" but not "hey stack"

+1  A: 
import itertools

# Adapted from Python Cookbook 2nd Ed. 19.7.
def windows(iterable, length=2, overlap=0):
    """
    Return an iterator over overlapping windows of length <length> of <iterable>.
    """
    it = iter(iterable)
    results = list(itertools.islice(it, length))
    while len(results) == length:
        yield results
        results = results[length-overlap:]
        results.extend(itertools.islice(it, length-overlap))

def magic_function(seq):
    return [' '.join(window) for n in range(len(words)) for window in windows(seq, n + 1, n)]

Results:

>>> words = ["hey","there","stack","overflow"]
>>> print magic_function(words)
['hey', 'there', 'stack', 'overflow', 'hey there', 'there stack', 'stack overflow', 'hey there stack', 'there stack overflow', 'hey there stack overflow']
Jon-Eric
+1  A: 

I think something like this will work, although I don't have access to python at the moment.

def magic_function(words):
  for start in range(len(words)):
    for end in range(start + 1, len(words) + 1):
      yield " ".join(words[start:end])
recursive
A: 

This will work, seems reasonably efficient.

def magicFunction(words):
    phrases = []
    start = 0
    end = 0
    for i in xrange(1, len(words) + 1):
        start = 0
        end = i
        while (end <= len(words)):
            phrases.append(" ".join(words[start:end]))
            start += 1
            end += 1
    return phrases
Charles Ma