使用字典替换字符串中的单词和replace()函数的问题 [英] Issues with replacing words in a string using a dictionary and the replace() function
问题描述
the_dictionary = {'我的':'你的','我':'你','你的' '我''''''我''
the_string ='我以为这是你的
list_string = ['我','想' ','was','yours']
这是我的代码:
pre>
for list_string中的单词:
如果word中的word为
the_string = the_string.replace(word,the_dictionary [word],1 )
print(the_string)
输入:我以为是你的
输出:你以为是我的
这里一切都很好,但如果我将输入更改为:
the_string =这是我的这是你的
输入:这是我的这是你的
输出: / em>这是我的这是你的
没有c挂起。
显然与事实上它们是一个键值对,但我希望这可以解决某种程度。
我的问题:为什么会发生这种情况?
请记住,我仍然是一个初学者,如果你可以假装我是孩子,同时解释它,感激不尽。
感谢您选择时间/ wazus
问题是您正在调用每次都在
,当用可选参数调用 the_string
中替换替换
替换源字符串的第一次出现。
所以,第一次遇到我
在 list_string
, the_string
被更改为这是你的这个是你的
。到目前为止,这是预期的。
但是后来,您在中遇到
,你说 yours
list_string the_string = the_string.replace('yours','mine',1)
。所以,在 the_string
中第一次出现 yours
将被替换为 mine
,这使我们回到原来的字符串。
这是一种解决方法:
<$ p $在[78]中:the_string =这是我的这是你的
在[79]:the_dictionary = {'我的':'你的','我' :'你','你''''''''''''
在[80]:list_string = the_string.split()
[81]:for i,word in enumerate(list_string):
if word in the_dictionary:
list_string [i] = the_dictionary [word]
....:
在[82]:print(''.join(list_string))
这是你的是我的
Say I have a dictionary, a string and a list of the words in that string. Like this:
the_dictionary={'mine': 'yours', 'I': 'you', 'yours': 'mine', 'you': 'I'}
the_string='I thought that was yours'
list_string=['I','thought','that','was','yours']
This is my code:
for word in list_string:
if word in the_dictionary:
the_string=the_string.replace(word,the_dictionary[word],1)
print(the_string)
input: I thought that was yours
Output: you thought that was mine
Here everything works great, but if I change the input to:
the_string="That is mine that is yours"
Input: That is mine that is yours
Output: That is mine that is yours
Nothing changes.
Obviously it has something to do with the fact that they are a key-value pair but my hope is that this can be solved somehow.
My question: Why does this happen and can it be fixed?
Please keep in mind that I am still sort of a beginner and would appreciate it if you could pretend I am child while explaining it.
Thanks for taking the time /wazus
The issue is that you are calling replace
on the_string
each time, and when called with the optional argument, replace
replaces the first occurrences of the source string.
So, the first time you encounter mine
in list_string
, the_string
gets changed to That is yours that is yours
. So far, this is what is expected.
But later, you encounter yours
in list_string
, and you say the_string = the_string.replace('yours', 'mine', 1)
. So, the first occurrence of yours
in the_string
gets replaced with mine
, which brings us back to the original string.
Here's one way to fix it:
In [78]: the_string="That is mine that is yours"
In [79]: the_dictionary={'mine': 'yours', 'I': 'you', 'yours': 'mine', 'you': 'I'}
In [80]: list_string = the_string.split()
In [81]: for i,word in enumerate(list_string):
if word in the_dictionary:
list_string[i] = the_dictionary[word]
....:
In [82]: print(' '.join(list_string))
That is yours that is mine
这篇关于使用字典替换字符串中的单词和replace()函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!