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

Sodmzs

例如,我有一段文字说

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

关于此的任何帮助,将非常可观。

安德烈·凯斯利(Andrej Kesely)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

pandas agg 在某些列中连接字符串,同时在其他列中保持最大值

反转字符串字符,同时将它们保持在相同位置

在 python beautifulsoup 中查找与其他字符串共享相同类的字符串

将文本单元格中的字符串与其他单元格中(相同位置-分隔符)的其他字符串连接在一起

在不同位置的字符串中插入字符

为什么Django .po中的某些字符串会被翻译,而其他的则不会?

如何在notepad ++中保留特殊字符串并删除其他字符串?

在不同位置的其他字符串中,第一个字符串中有多少个数字

管理 Python 字符串中的关键字参数:如何仅格式化某些参数并保持其他参数未格式化

如何使字符串在角度4中保持不变?

如何通过python中的.replace方法用其他字符串替换字符串?

在for循环的某些迭代中,字符串的长度为0,而在其他迭代中则不是

Python:如何对字符串中的字母进行排序以使某些字符保持原状?

尝试用 TypeScript 中字符串中的其他字符替换某些字符

如何将熊猫数据框中的字符串设置为所有行中的相同位置?

打印两个字符串中相同且出现在相同位置的字符数

Python - 在扩展其他字符串的同时保持字符串完整

如何在 Python 中的字符串中获取多个相同的字符及其位置?

如何最好地播放随机字符串,某些字符串的权重比其他字符串重

如何在C#字符串中替换其他字符而忽略其他字符?

如何连接二维numpy字符串数组的相同位置元素?

在相同位置的html DOM元素中替换后转换字符串

python如何大写字符串中的某些字符

在python中,如何从字符串中提取某些字符?

如何保持相同的字符串长度?

如何用其他Python替换字符串中的unicode字符?

如何用其他Python替换字符串中的unicode字符?

用Python方式在不同位置连接字符串

按相同位置的字符过滤字符串列表