[英]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
的解决方案,因为为什么不:
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.