required_output = [‘happy dinner’,’happy lunch’,’sad dinner’, ‘sad lunch’]
我需要生成所有可能的句子。
{{1}}
但我总是没有2个名单。有时我有3,4,6,8等名单。我需要帮助在python中动态生成所有可能的句子。谢谢!!
答案 0 :(得分:1)
您可以使用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']
答案 1 :(得分:0)
在多个列表案例中你可以这样做
试试这个: -
import itertools
final_list = [list1,list2,list3,....]
print(list(itertools.product(*final_list))) #you will get all possible matches
答案 2 :(得分:0)
我可以提出一个想法,假设我有4个列表,每个列表都包含单词。现在,而不是直接组合它。我们可以先找到列表的所有组合。 2个单词句子,3个单词和4个单词的组合。
所有索引组合的集合都在index_combin
列表中。这对您的数据也很有用。
另外,我们将使用它来从4个列表中获得2,3和4组合。这存储在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')
您还可以对这些单词集进行置换。