简体   繁体   English

如何使用 python 中的字典将多个单词替换为单个单词?

[英]How to replace multiple words with single word using dictionary in python?

I have a dictionary of keys and multiple values as below:我有一个键和多个值的字典,如下所示:

word_list = {'cool':['better','good','best','great'], 'car':['vehicle','moving','automobile','four-wheeler'], 'sound':['noise', 'disturbance', 'rattle']}
sentences = ['that day I heard a vehicle noise not a great four-wheeler', 'the automobile industry is doing good these days', 'that moving noise is better now']

As I have multiple values for a given key, if any of these values appear in the sentences, I want to replace them with its associated key.由于给定键有多个值,如果这些值中的任何一个出现在句子中,我想用其关联的键替换它们。

I tried the following, but did not get the desired output.我尝试了以下,但没有得到想要的 output。

results= [' '.join(word_list.get(y, y) for y in w.split()) for w in sentences]

Desired output:所需的 output:

['that day I heard a car sound not a cool car', 'the car industry is doing cool these days', 'that car sound is better now']

Not sure how to achieve this.不知道如何实现这一点。

The trick is actually to create an inverted mapping where you set as key, each value of the replacement key, and as value the key.诀窍实际上是创建一个反向映射,您可以在其中将替换键的每个值设置为键,并将键的值设置为值。

Then after it's easy, as you just have to iterate on each word in each sentence and replace it with the value of that inverted mapping, if the word is one of the key of this mapping.然后很简单,因为您只需要迭代每个句子中的每个单词并将其替换为该反向映射的值,如果该单词是该映射的键之一。

word_list = {
    'cool': ['better','good','best','great'],
    'car': ['vehicle','moving','automobile','four-wheeler'],
    'sound': ['noise', 'disturbance', 'rattle']
}
sentences = [
    'that day I heard a vehicle noise not a great four-wheeler',
    'the automobile industry is doing good these days',
    'that moving noise is better now'
]

swapped_word_list = {
    word: replacement
    for replacement, words in word_list.items()
    for word in words
}
new_sentences = [
    ' '.join([
        swapped_word_list.get(word, word)
        for word in sentence.split()
    ])
    for sentence in sentences
]

a solution using regex & reduce , because why not:使用regex & reduce的解决方案,因为为什么不:

  • create a list of mapping for a regex pattern matching all the words to the replacement word为将所有单词与替换单词匹配的正则表达式模式创建映射列表
  • apply all the mappings to each string recursively using reduce使用reduce递归地将所有映射应用于每个字符串

note the prefix rf before the string specifies that it is a raw f-string注意字符串之前的前缀rf指定它是原始 f 字符串

from functools import reduce
import re

mappings = [
  {'pat': rf'\b({"|".join(words)})\b', 'rep': rep}
  for rep, words in word_list.items()
]

cleaned_sentences = [
  reduce(lambda s, m: re.sub(m['pat'], m['rep'], s), mappings, sentence)
  for sentence in sentences
]
for s in cleaned_sentences:
  print(s)
# outputs:
that day I heard a car sound not a cool car
the car industry is doing cool these days
that car sound is cool now

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用仅代表单个单词而不代表多个单词的通配符在 Python 中搜索字符串? - How to search strings in Python using a wildcard that represents only a single word -- and not multiple words as well? 用字典替换字符串中的多个单词(python) - Replace multiple words in string with dictionary (python) 使用字典随机替换python中的某些单词 - Randomly replace certain words in python using a dictionary 如何使用字典替换字符串中的复合词? - How to replace compound words in a string using a dictionary? 如何使用字典映射替换字符串中的单词 - How to replace words in a string using a dictionary mapping 如何使用基于句子的字典替换单词 - How to replace words using dictionary based on sentences 如何在python中使用正则表达式替换字符串的多个单词? - How to replace multiple words of a string using regex in python? 如何在python中用识别的下一个单词查找和替换段落中的多个单词? - How to find and replace multiple words from paragraph with identified next word in python? 如何使用包含多个键值的字典在python中替换字符串 - How to replace a string using a dictionary containing multiple values for a key in python 如何使用Python删除特定单词之前的所有单词(如果有多个特定单词)? - How to remove all words before specific word using Python (if there are multiple specific words)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM