现在,我正在使用 itertools
为网站创建数千种元描述变体。句子结构如下:
(SentencePart1a
|SentencePart1b
) 关键字1
、关键字2
和关键字3
。 (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
。
我试图尽量减少最终列表中的冗余。
最佳答案
只需根据关键字执行产品,而不是根据句子。
在开始和结束句子上使用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.
关于python - 在 python 3.5 中使用 itertools,如何仅创建某些列表的某些变体,但创建其他列表的所有变体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41069740/