在python 3.5中使用itertools,我如何只创建一些列表的某些变体,但是其他列表的变体?

时间:2016-12-09 22:17:46

标签: python python-3.x

现在我正在使用itertools为网站创建数千种元描述变体。句子结构如下:

SentencePart1a | SentencePart1bKeyword1Keyword2Keyword3。 (MiddleSentenceA | MiddleSentenceB)。 (FinalSentenceA | FinalSentenceB)。

脚本的简短版本:

import itertools
metas = [
['Shop our store for ', 'Browse our shop for ,'], #SentenceParts
['keyword,', 'keyword,', 'etc,'],
['keyword, and', 'keyword, and', 'etc, and'], 
['keyword.', 'keyword.', 'etc.'], 
['Sentence about our store.', 'A different sentence about our store.'], #MiddleSentences
['A final sentence about our store.', 'A different final sentence about our store.'] #FinalSentences
    ]
variantmetas = list(itertools.product(*metas))
#print (variantmetas)
for s in variantmetas:
print(' '.join(s))

现在我得到了所有这些东西的每一个变化。即使关键字1-3是相同的,我的程序也会吐出所有句子部分,所有中间句子和所有最终句子。

如何设置关键字1-3只显示一次,并且句子部分随机变化?换句话说:关键字的所有变体只有一次,一个SentencePart,一个MiddleSentence和一个FinalSentence

我正在努力减少最终列表中的冗余。

1 个答案:

答案 0 :(得分:1)

只需在关键字上执行产品,而不是在句子上执行。

在开始时使用random.choice&结束句子,并使用列表理解生成结果。

提案没有重新组织metas列表,这可能更好地将关键词与开始/结束句子隔离开来:

import itertools,random
metas = [
['Shop our store for ', 'Browse our shop for ,'], #SentenceParts
['keyword,', 'keyword,', 'etc,'],
['keyword, and', 'keyword, and', 'etc, and'],
['keyword.', 'keyword.', 'etc.'],
['Sentence about our store.', 'A different sentence about our store.'], #MiddleSentences
['A final sentence about our store.', 'A different final sentence about our store.'] #FinalSentences
    ]
variantmetas = [[random.choice(metas[0])]+list(l)+[random.choice(metas[-2]),random.choice(metas[-1])] for l in itertools.product(*metas[1:-2])]
for s in variantmetas:
    print(' '.join(s))

结果摘录:

Shop our store for  etc, keyword, and keyword. Sentence about our store. A different final sentence about our store.
Browse our shop for , etc, keyword, and etc. Sentence about our store. A different final sentence about our store.
Browse our shop for , etc, etc, and keyword. Sentence about our store. A final sentence about our store.
Shop our store for  etc, etc, and keyword. Sentence about our store. A different final sentence about our store.
Shop our store for  etc, etc, and etc. Sentence about our store. A final sentence about our store.