Home > database >  Python get all possibilities from word's list to create a paragraph
Python get all possibilities from word's list to create a paragraph

Time:10-21

i want a script can generate all possibilities from word's list

example:

array :

["Ford", "Volvo", "BMW" ,'word1', 'word2', 'word4','word7', 'word9', 'word5','word100', 'wordaaaaa', 'words'...........etc]

to a paragraph contain just 5 words like:

word2 word1 Volvo BMW wordaaaaa
words word9 Volvo Ford BMW
word100 word1 Volvo BMW word7

and get all possibilities to create a paragraph contain 5 words

CodePudding user response:

you can use itertools.combinations:

from itertools import combinations

words = ["Ford", "Volvo", "BMW" ,'word1', 'word2', 'word4','word7', 'word9', 'word5','word100', 'wordaaaaa', 'words']

for c in combinations(words, 5):
    print(" ".join(c))

Output:

Ford Volvo BMW word1 word2
Ford Volvo BMW word1 word4
Ford Volvo BMW word1 word7
Ford Volvo BMW word1 word9
Ford Volvo BMW word1 word5
Ford Volvo BMW word1 word100
Ford Volvo BMW word1 wordaaaaa
...
  • Related