let's say I have 4 lists each list contains 4 words
List 1: plane fight door chair
List 2: egg spoon car bike
List 3: fire dog truck stairs
List 4: leg water shoe air
How can I use python to generate n number of phrases out of those lists while using one word from each list at a time and keep them in order to have a result similar to this:
plane spoon fire air
plane egg truck leg
door spoon stairs water
I'm not a dev, I hope you understood the general idea, can you please help me
CodePudding user response:
Use itertools.product
to find the product of a set of iterables. Use str.join
to convert each product back into a single string of space-separated words.
from itertools import product
word_lists = [words.split() for words in (
"plane fight door chair",
"egg spoon car bike",
"fire dog truck stairs",
"leg water shoe air",
)]
for p in product(*word_lists):
print(" ".join(p))
prints:
plane egg fire leg
plane egg fire water
plane egg fire shoe
plane egg fire air
plane egg dog leg
plane egg dog water
plane egg dog shoe
plane egg dog air
plane egg truck leg
plane egg truck water
plane egg truck shoe
plane egg truck air
plane egg stairs leg
plane egg stairs water
plane egg stairs shoe
plane egg stairs air
plane spoon fire leg
plane spoon fire water
plane spoon fire shoe
plane spoon fire air
plane spoon dog leg
plane spoon dog water
plane spoon dog shoe
plane spoon dog air
plane spoon truck leg
plane spoon truck water
plane spoon truck shoe
plane spoon truck air
plane spoon stairs leg
plane spoon stairs water
plane spoon stairs shoe
plane spoon stairs air
plane car fire leg
plane car fire water
plane car fire shoe
plane car fire air
plane car dog leg
plane car dog water
plane car dog shoe
plane car dog air
plane car truck leg
plane car truck water
plane car truck shoe
plane car truck air
plane car stairs leg
plane car stairs water
plane car stairs shoe
plane car stairs air
plane bike fire leg
plane bike fire water
plane bike fire shoe
plane bike fire air
plane bike dog leg
plane bike dog water
plane bike dog shoe
plane bike dog air
plane bike truck leg
plane bike truck water
plane bike truck shoe
plane bike truck air
plane bike stairs leg
plane bike stairs water
plane bike stairs shoe
plane bike stairs air
fight egg fire leg
fight egg fire water
fight egg fire shoe
fight egg fire air
fight egg dog leg
fight egg dog water
fight egg dog shoe
fight egg dog air
fight egg truck leg
fight egg truck water
fight egg truck shoe
fight egg truck air
fight egg stairs leg
fight egg stairs water
fight egg stairs shoe
fight egg stairs air
fight spoon fire leg
fight spoon fire water
fight spoon fire shoe
fight spoon fire air
fight spoon dog leg
fight spoon dog water
fight spoon dog shoe
fight spoon dog air
fight spoon truck leg
fight spoon truck water
fight spoon truck shoe
fight spoon truck air
fight spoon stairs leg
fight spoon stairs water
fight spoon stairs shoe
fight spoon stairs air
fight car fire leg
fight car fire water
fight car fire shoe
fight car fire air
fight car dog leg
fight car dog water
fight car dog shoe
fight car dog air
fight car truck leg
fight car truck water
fight car truck shoe
fight car truck air
fight car stairs leg
fight car stairs water
fight car stairs shoe
fight car stairs air
fight bike fire leg
fight bike fire water
fight bike fire shoe
fight bike fire air
fight bike dog leg
fight bike dog water
fight bike dog shoe
fight bike dog air
fight bike truck leg
fight bike truck water
fight bike truck shoe
fight bike truck air
fight bike stairs leg
fight bike stairs water
fight bike stairs shoe
fight bike stairs air
door egg fire leg
door egg fire water
door egg fire shoe
door egg fire air
door egg dog leg
door egg dog water
door egg dog shoe
door egg dog air
door egg truck leg
door egg truck water
door egg truck shoe
door egg truck air
door egg stairs leg
door egg stairs water
door egg stairs shoe
door egg stairs air
door spoon fire leg
door spoon fire water
door spoon fire shoe
door spoon fire air
door spoon dog leg
door spoon dog water
door spoon dog shoe
door spoon dog air
door spoon truck leg
door spoon truck water
door spoon truck shoe
door spoon truck air
door spoon stairs leg
door spoon stairs water
door spoon stairs shoe
door spoon stairs air
door car fire leg
door car fire water
door car fire shoe
door car fire air
door car dog leg
door car dog water
door car dog shoe
door car dog air
door car truck leg
door car truck water
door car truck shoe
door car truck air
door car stairs leg
door car stairs water
door car stairs shoe
door car stairs air
door bike fire leg
door bike fire water
door bike fire shoe
door bike fire air
door bike dog leg
door bike dog water
door bike dog shoe
door bike dog air
door bike truck leg
door bike truck water
door bike truck shoe
door bike truck air
door bike stairs leg
door bike stairs water
door bike stairs shoe
door bike stairs air
chair egg fire leg
chair egg fire water
chair egg fire shoe
chair egg fire air
chair egg dog leg
chair egg dog water
chair egg dog shoe
chair egg dog air
chair egg truck leg
chair egg truck water
chair egg truck shoe
chair egg truck air
chair egg stairs leg
chair egg stairs water
chair egg stairs shoe
chair egg stairs air
chair spoon fire leg
chair spoon fire water
chair spoon fire shoe
chair spoon fire air
chair spoon dog leg
chair spoon dog water
chair spoon dog shoe
chair spoon dog air
chair spoon truck leg
chair spoon truck water
chair spoon truck shoe
chair spoon truck air
chair spoon stairs leg
chair spoon stairs water
chair spoon stairs shoe
chair spoon stairs air
chair car fire leg
chair car fire water
chair car fire shoe
chair car fire air
chair car dog leg
chair car dog water
chair car dog shoe
chair car dog air
chair car truck leg
chair car truck water
chair car truck shoe
chair car truck air
chair car stairs leg
chair car stairs water
chair car stairs shoe
chair car stairs air
chair bike fire leg
chair bike fire water
chair bike fire shoe
chair bike fire air
chair bike dog leg
chair bike dog water
chair bike dog shoe
chair bike dog air
chair bike truck leg
chair bike truck water
chair bike truck shoe
chair bike truck air
chair bike stairs leg
chair bike stairs water
chair bike stairs shoe
chair bike stairs air
CodePudding user response:
If you don't care about words being chosen multiple times, you can do something simple like
from random import randint
# our lists
list1 = ['plane', 'fight', 'door', 'chair']
list2 = ['egg', 'spoon', 'car', 'bike']
list3 = ['fire', 'dog', 'truck', 'stairs']
list4 = ['leg', 'water', 'shoe', 'air']
# however many times you want to run the code
n = 5
# running this for loop 'n' times
for _ in range(n):
# our four random indices
i = randint(0, 3)
j = randint(0, 3)
k = randint(0, 3)
l = randint(0, 3)
# an aggregate based on our 4 lists
list_builder = [list1[i], list2[j], list3[k], list4[l]]
print(list_builder)