使用dict替换字符串中的单词

时间:2020-12-28 19:22:01

Im trying to replace one part of my string using a dict.

我试图用dict替换我的字符串的一部分。

s  = 'I am a string replaceme'

d = {
'replaceme':  'replace me'
 }

Ive tried lots of variations like

我尝试了很多变化

s = s.replace(d, d[other])

That throws an error being name error: name 'other' is not defined. If I do

这会引发错误,因为名称错误:名称'other'未定义。如果我做

s = s.replace('replaceme', 'replace me')

It works. How can i achive my goal?

有用。我怎样才能实现我的目标?

3 个解决方案

#1


2  

You have to replace each KEY of your dict with the VALUE associated. Which value holds the other variable? Is it a valid KEY of your substitutions dict? You can try with this solution.

您必须使用相关的VALUE替换dict的每个KEY。哪个值保存另一个变量?它是你的替换词的有效键吗?您可以尝试使用此解决方案。

for k in d:
    s = s.replace(k, d[k])

Each key in dictionary is the value to be replaced, using the corresponding VALUE accessed with d[k].
If the dictionary is big the provided example will show poor performances.

字典中的每个键都是要替换的值,使用d [k]访问的相应VALUE。如果字典很大,则提供的示例将显示性能差。

#2


0  

You could split the string and rejoin:

您可以拆分字符串并重新加入:

s = 'I am a string replaceme'

d = {
'replaceme':  'replace me'
 }

print(" ".join([w if w not in d else d[w] for w in s.split(" ")]))

That won't match substrings where str.replace will, if you are trying to match substring iterate over the dict.items and replace the key with the value:

如果你试图匹配dict.items上的子字符串迭代并将值替换为值,那将与str.replace所在的子字符串不匹配:

d = {
'replaceme':  'replace me'
 }

for k,v in d.items():
    s = s.replace(k,v)

print(s)

I am a string replace me

#3


0  

Here is a different approach: using reduce:

这是一种不同的方法:使用reduce:

s  = 'I am a string replaceme'
d = {'replaceme': 'replace me', 'string': 'phrase,'}
s = reduce(lambda text, old_new_pair: text.replace(* old_new_pair), d.items(), s)
# s is now 'I am a phrase, replace me'

#1


2  

You have to replace each KEY of your dict with the VALUE associated. Which value holds the other variable? Is it a valid KEY of your substitutions dict? You can try with this solution.

您必须使用相关的VALUE替换dict的每个KEY。哪个值保存另一个变量?它是你的替换词的有效键吗?您可以尝试使用此解决方案。

for k in d:
    s = s.replace(k, d[k])

Each key in dictionary is the value to be replaced, using the corresponding VALUE accessed with d[k].
If the dictionary is big the provided example will show poor performances.

字典中的每个键都是要替换的值,使用d [k]访问的相应VALUE。如果字典很大,则提供的示例将显示性能差。

#2


0  

You could split the string and rejoin:

您可以拆分字符串并重新加入:

s = 'I am a string replaceme'

d = {
'replaceme':  'replace me'
 }

print(" ".join([w if w not in d else d[w] for w in s.split(" ")]))

That won't match substrings where str.replace will, if you are trying to match substring iterate over the dict.items and replace the key with the value:

如果你试图匹配dict.items上的子字符串迭代并将值替换为值,那将与str.replace所在的子字符串不匹配:

d = {
'replaceme':  'replace me'
 }

for k,v in d.items():
    s = s.replace(k,v)

print(s)

I am a string replace me

#3


0  

Here is a different approach: using reduce:

这是一种不同的方法:使用reduce:

s  = 'I am a string replaceme'
d = {'replaceme': 'replace me', 'string': 'phrase,'}
s = reduce(lambda text, old_new_pair: text.replace(* old_new_pair), d.items(), s)
# s is now 'I am a phrase, replace me'