Skip to content

Instantly share code, notes, and snippets.

@nimish-mehta
Last active August 29, 2015 13:57
Show Gist options
  • Save nimish-mehta/9426203 to your computer and use it in GitHub Desktop.
Save nimish-mehta/9426203 to your computer and use it in GitHub Desktop.
import itertools
def getAllSentences(input_data):
"""
take cartesian cross product of list and join to form the sentences to get all sentences.
Assumption: The order for sentences is that for every element in ith list create all sentences from i-1..0th list
i.e. if list = [[1,2],[3,4],[5]]
order of sentences = [135,145,235,245]
"""
return [" ".join(sentence) for sentence in list(itertools.product(*input_data))]
#for testing the function
input_data = [ ['Birds', 'Animals', 'Humans'],
['are'] ,
['angry' , 'happy']]
for sentence in getAllSentences(input_data):
print sentence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment