简体   繁体   English

如何从单词表中生成短语

[英]How do I generate phrases out of wordlist

let's say I have 4 lists each list contains 4 words假设我有 4 个列表,每个列表包含 4 个单词

List 1: plane fight door chair清单一:飞机拼门椅

List 2: egg spoon car bike清单2:蛋勺汽车自行车

List 3: fire dog truck stairs清单 3:消防犬卡车楼梯

List 4: leg water shoe air清单4:腿水鞋气

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:如何使用 python 从这些列表中生成 n 个短语,同时一次使用每个列表中的一个单词并保留它们以获得类似于以下的结果:

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我不是开发人员,我希望你能理解大致的想法,你能帮帮我吗

Use itertools.product to find the product of a set of iterables.使用itertools.product查找一组可迭代对象的乘积。 Use str.join to convert each product back into a single string of space-separated words.使用str.join将每个产品转换回单个空格分隔的单词字符串。

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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM