[英]How to create multiple sentences from a given sentence in Python
我想通過在 python 中混淆單詞來從給定的句子創建多個句子。
例如說我有一句話“密碼重置成功”
現在,我需要從上面的句子中生成各種組合。
輸出:
reset password successful
successful reset password
successful password reset
password reset successful.
...
如何使用python
您可以使用itertools.permutations
:
>>> from itertools import permutations
>>> sentence = "password reset successful"
>>> for sent in permutations(sentence.split()):
print(' '.join(sent))
password reset successful
password successful reset
reset password successful
reset successful password
successful password reset
successful reset password
如果你想在一個列表中:
>>> [' '.join(sent) for sent in permutations(sentence.split())]
['password reset successful',
'password successful reset',
'reset password successful',
'reset successful password',
'successful password reset',
'successful reset password']
如果您不希望列表中的原始sentence
:
>>> combs = [' '.join(sent) for sent in permutations(sentence.split())]
>>> combs.remove(sentence)
>>> combs
['password successful reset',
'reset password successful',
'reset successful password',
'successful password reset',
'successful reset password']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.