如何迭代字符串中的某些字符和其他字符留在 python 中的相同位置

How to iterate some characters in a string and other character stay on the same position in python

我有一个文本说例如,

t = "The climate is super awesome"

通过做,

from nltk.tokenize import word_tokenize words = word_tokenize(t)

我得到了什么,

>>>words = ["The","climate","is","super","awesome"]

我在字典中有多个列表,每个列表都有一个同义词列表。 例如,

dict = {'climate' : [weather,region,zone], 'super' : [excellent, superior, outstanding], 'awesome' : [amazing,great,stunning]}

如何编写代码来获取句子中同义词的排列组合。 假设我们的每个单词至少或恰好有 3 个已识别的同义词。 那么't'选中的第一行一共有3个词。 因此3的3次方=可以生成27个句子

以及我想要的输出结果如何?

The weather is excellent amazing
The weather is excellent great
The weather is excellent stunning
The weather is superior amazing
The weather is superior great
The weather is superior stunning
The weather is outstanding amazing
The weather is outstanding great
The weather is outstanding stunning
The region is excellent amazing
The region is excellent great
The region is excellent stunning
The region is superior amazing
The region is superior great
The region is superior stunning
The region is outstanding amazing
The region is outstanding great
The region is outstanding stunning
The zone is excellent amazing
The zone is excellent great
The zone is excellent stunning
The zone is superior amazing
The zone is superior great
The zone is superior stunning
The zone is outstanding amazing
The zone is outstanding great
The zone is outstanding stunning

如能提供任何帮助,我们将不胜感激。

如果您的输入字典始终采用上述形式,您可以简单地执行以下操作:

for x in d["climate"]:
    for y in d["super"]:
        for z in d["awesome"]:
            print("The {} is {} {}".format(x, y, z))

我试过了,似乎有效:

import re
words = ["The","climate","is","super","awesome"]
synons = {'climate' : ['weather','region','zone'], 'super' : ['excellent', 'superior', 'outstanding'], 'awesome' : ['amazing','great','stunning']}

phrases = {' '.join(words)}
for word, synonyms in synons.items():
    for syn in synonyms:
        for p in list(phrases):
            phrases.add(re.sub(r"\b%s\b" % word, syn, p))

print('\n'.join(sorted(phrases)))

输出:

The climate is excellent amazing
The climate is excellent awesome
The climate is excellent great
The climate is excellent stunning
The climate is outstanding amazing
The climate is outstanding awesome
The climate is outstanding great
The climate is outstanding stunning
The climate is super amazing
The climate is super awesome
The climate is super great
The climate is super stunning
The climate is superior amazing
The climate is superior awesome
The climate is superior great
The climate is superior stunning
The region is excellent amazing
The region is excellent awesome
The region is excellent great
The region is excellent stunning
The region is outstanding amazing
The region is outstanding awesome
The region is outstanding great
The region is outstanding stunning
The region is super amazing
The region is super awesome
The region is super great
The region is super stunning
The region is superior amazing
The region is superior awesome
The region is superior great
The region is superior stunning
The weather is excellent amazing
The weather is excellent awesome
The weather is excellent great
The weather is excellent stunning
The weather is outstanding amazing
The weather is outstanding awesome
The weather is outstanding great
The weather is outstanding stunning
The weather is super amazing
The weather is super awesome
The weather is super great
The weather is super stunning
The weather is superior amazing
The weather is superior awesome
The weather is superior great
The weather is superior stunning
The zone is excellent amazing
The zone is excellent awesome
The zone is excellent great
The zone is excellent stunning
The zone is outstanding amazing
The zone is outstanding awesome
The zone is outstanding great
The zone is outstanding stunning
The zone is super amazing
The zone is super awesome
The zone is super great
The zone is super stunning
The zone is superior amazing
The zone is superior awesome
The zone is superior great
The zone is superior stunning

假设 dict 被(更恰当地)命名为 synonyms,并且您清除了它的语法错误,这应该可以完成工作:

import itertools
for x in itertools.product(*[synonyms.get(x,[x]) for x in words]):
    print(" ".join(x))

使用 itertools.productstr.replace:

words = ["The","climate","is","super","awesome"]
synonyms = {'climate' : ['weather','region','zone'],
            'super' : ['excellent', 'superior', 'outstanding'],
            'awesome' : ['amazing','great','stunning']}

from itertools import product

s = ' '.join(words)

for val in product(*[[(k, i) for i in v] for k, v in synonyms.items()]):
    new_s = s
    for (orig, new_one) in val:
        new_s = new_s.replace(orig, new_one)
    print(new_s)

打印:

The weather is excellent amazing
The weather is excellent great
The weather is excellent stunning
The weather is superior amazing
The weather is superior great
The weather is superior stunning
The weather is outstanding amazing
The weather is outstanding great
The weather is outstanding stunning
The region is excellent amazing
The region is excellent great
The region is excellent stunning
The region is superior amazing
The region is superior great
The region is superior stunning
The region is outstanding amazing
The region is outstanding great
The region is outstanding stunning
The zone is excellent amazing
The zone is excellent great
The zone is excellent stunning
The zone is superior amazing
The zone is superior great
The zone is superior stunning
The zone is outstanding amazing
The zone is outstanding great
The zone is outstanding stunning