簡體   English   中英

使用Python從“ N”個列表中生成所有可能的句子

[英]Generate all possible sentences from “N” lists using Python

list_1 = ['happy','sad']
list_2 = ['dinner','lunch']

我需要生成所有可能的句子。

required_output = [‘happy dinner’,’happy lunch’,’sad dinner’, ‘sad lunch’]

但是我總是只有兩個列表。 有時我有3、4、6、8等列表。 我需要幫助以python動態生成所有可能的句子。 謝謝 !!

您可以使用itertools.product

import itertools
list_1 = ['happy','sad'] 
list_2 = ['dinner','lunch'] 
l = [list_1, list_2]
final_list = ["{} {}".format(*i) for i in itertools.product(*l, repeat = 2)]

輸出:

['happy dinner', 'happy lunch', 'sad dinner', 'sad lunch']

在多個列表的情況下,您可以這樣做

嘗試這個:-

import itertools
final_list = [list1,list2,list3,....]
print(list(itertools.product(*final_list))) #you will get all possible matches

我可以提出一個想法,假設我有4個列表,每個列表包含單詞。 現在,而不是直接組合它。.我們首先可以找到列表的所有組合。 2詞句子,3詞..和4詞的組合

所有索引組合的集合在index_combin列表中。 這對於您的數據也很有用。

另外,我們將使用它從4個列表中獲得2、3和4組合a。 這存儲在sets

這是代碼:

import itertools as it

list_1 = ['happy','sad'];
list_2 = ['dinner','lunch'];
list_3 = ['indoor', 'outdoor', 'restaurant'];
list_4 = ['alone', 'together'];
combined = [list_1, list_2, list_3, list_4];

index_combin = [];
words_combin = [];

for i in range(2,len(combined)+1):
    dummy = it.combinations(range(0, len(combined)),i);
    index_combin.append(list(dummy));

sets = [];
for i in index_combin:
    for j in i:
        sets.append([combined[k] for k in j]);

for i in sets:
    for j in list(it.product(*i)):
        print(j)

輸出:

('happy', 'dinner')
('happy', 'lunch')
('sad', 'dinner')
('sad', 'lunch')
('happy', 'indoor')
('happy', 'outdoor')
('happy', 'restaurant')
('sad', 'indoor')
('sad', 'outdoor')
('sad', 'restaurant')
('happy', 'alone')
('happy', 'together')
('sad', 'alone')
('sad', 'together')
('dinner', 'indoor')
('dinner', 'outdoor')
('dinner', 'restaurant')
('lunch', 'indoor')
('lunch', 'outdoor')
('lunch', 'restaurant')
('dinner', 'alone')
('dinner', 'together')
('lunch', 'alone')
('lunch', 'together')
('indoor', 'alone')
('indoor', 'together')
('outdoor', 'alone')
('outdoor', 'together')
('restaurant', 'alone')
('restaurant', 'together')
('happy', 'dinner', 'indoor')
('happy', 'dinner', 'outdoor')
('happy', 'dinner', 'restaurant')
('happy', 'lunch', 'indoor')
('happy', 'lunch', 'outdoor')
('happy', 'lunch', 'restaurant')
('sad', 'dinner', 'indoor')
('sad', 'dinner', 'outdoor')
('sad', 'dinner', 'restaurant')
('sad', 'lunch', 'indoor')
('sad', 'lunch', 'outdoor')
('sad', 'lunch', 'restaurant')
('happy', 'dinner', 'alone')
('happy', 'dinner', 'together')
('happy', 'lunch', 'alone')
('happy', 'lunch', 'together')
('sad', 'dinner', 'alone')
('sad', 'dinner', 'together')
('sad', 'lunch', 'alone')
('sad', 'lunch', 'together')
('happy', 'indoor', 'alone')
('happy', 'indoor', 'together')
('happy', 'outdoor', 'alone')
('happy', 'outdoor', 'together')
('happy', 'restaurant', 'alone')
('happy', 'restaurant', 'together')
('sad', 'indoor', 'alone')
('sad', 'indoor', 'together')
('sad', 'outdoor', 'alone')
('sad', 'outdoor', 'together')
('sad', 'restaurant', 'alone')
('sad', 'restaurant', 'together')
('dinner', 'indoor', 'alone')
('dinner', 'indoor', 'together')
('dinner', 'outdoor', 'alone')
('dinner', 'outdoor', 'together')
('dinner', 'restaurant', 'alone')
('dinner', 'restaurant', 'together')
('lunch', 'indoor', 'alone')
('lunch', 'indoor', 'together')
('lunch', 'outdoor', 'alone')
('lunch', 'outdoor', 'together')
('lunch', 'restaurant', 'alone')
('lunch', 'restaurant', 'together')
('happy', 'dinner', 'indoor', 'alone')
('happy', 'dinner', 'indoor', 'together')
('happy', 'dinner', 'outdoor', 'alone')
('happy', 'dinner', 'outdoor', 'together')
('happy', 'dinner', 'restaurant', 'alone')
('happy', 'dinner', 'restaurant', 'together')
('happy', 'lunch', 'indoor', 'alone')
('happy', 'lunch', 'indoor', 'together')
('happy', 'lunch', 'outdoor', 'alone')
('happy', 'lunch', 'outdoor', 'together')
('happy', 'lunch', 'restaurant', 'alone')
('happy', 'lunch', 'restaurant', 'together')
('sad', 'dinner', 'indoor', 'alone')
('sad', 'dinner', 'indoor', 'together')
('sad', 'dinner', 'outdoor', 'alone')
('sad', 'dinner', 'outdoor', 'together')
('sad', 'dinner', 'restaurant', 'alone')
('sad', 'dinner', 'restaurant', 'together')
('sad', 'lunch', 'indoor', 'alone')
('sad', 'lunch', 'indoor', 'together')
('sad', 'lunch', 'outdoor', 'alone')
('sad', 'lunch', 'outdoor', 'together')
('sad', 'lunch', 'restaurant', 'alone')
('sad', 'lunch', 'restaurant', 'together')

您還可以置換這些單詞中的每組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM