如何迭代字符串中的某些字符,而其他字符在python中保持相同的位置

2024-09-28 22:28:38 发布

您现在位置: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

任何有关这方面的帮助,都将是非常可观的。你知道吗


Tags: thezoneisregionawesomeweathersupertokenize
3条回答

使用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

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

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

相关问题 更多 >