Supposing I have a string with several space-separated words, like
words = "foo bar baz qux"
If I want a list of the words, I can just call words.split()
and get
['foo','bar','baz','qux']
But if I want to get each word and each set of (adjacent) words, like
['foo bar baz qux', 'foo bar baz', 'bar baz qux',
'foo bar', 'bar baz', 'baz qux', 'foo', 'bar',
'baz', 'qux']
How can I go about this? I'm sure I can write a big ugly function that takes a string like words
and iterates over each set of adjacent elements to return the above, but I've a hunch there's a more elegant way to go about it. Is there?