开发者

Given a list of words, make a subset of phrases with them

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开发者_Python百科" but not "hey stack"


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])


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']


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜