[英]Replace strings in list from dictionary
I'm having some issues with my code:我的代码有一些问题:
word_list = { "hello" : "1", "bye" : "2"}
sentence= ['hello you, hows things', 'hello, good thanks']
for key, value in word_list.iteritems():
for i in sentence:
i = i.replace(key, value)
print i
Expected output = '1 you, how's things'
, '1, good thanks'
预期输出 = '1 you, how's things'
, '1, good thanks'
It currently does not replace any occurrences of hello
.它目前不会替换任何出现的hello
。 I'm wondering if my sentence loop is correct or not?我想知道我的句子循环是否正确? Printing i
after this prints out exactly what is in sentence
.在这之后打印i
准确打印出sentence
。
I guess the word_list
(try to rename the variable to word_dict
, I think that is more appropriate) has lots of items,我猜word_list
(尝试将变量重命名为word_dict
,我认为这更合适)有很多项目,
for index, data in enumerate(sentence):
for key, value in word_list.iteritems():
if key in data:
sentence[index]=data.replace(key, word_list[key])
working example from ipython
来自ipython
工作示例
In [1]: word_list = { "hello" : "1", "bye" : "2"}
In [2]: sentence = ['hello you, hows things', 'hello, good thanks']
In [3]: for index, data in enumerate(sentence):
...: for key, value in word_list.iteritems():
...: if key in data:
...: sentence[index]=data.replace(key, word_list[key])
...:
In [4]: sentence
Out[4]: ['1 you, hows things', '1, good thanks']
The replacement occurs on a variable inside the loop So nothing is changed in the sentence
list To fix this make a new list with the changed items in it替换发生在循环内的变量上所以sentence
列表中没有任何更改要解决此问题,请创建一个包含更改项目的新列表
word_list = { "hello" : "1"}
sentence= ['hello you, hows things', 'hello, good thanks']
newlist=[]
for key, value in word_list.items():
for i in sentence:
i = i.replace(key, value)
newlist.append(i)
print newlist
Another way with map地图的另一种方式
word_list = { "hello" : "1"}
sentence= ['hello you, hows things', 'hello, good thanks']
newlist=[]
for key, value in word_list.items():
newlist=map(lambda x: x.replace(key,value), sentence)
print newlist
Another way with a list comprehension列表理解的另一种方式
word_list = { "hello" : "1"}
sentence= ['hello you, hows things', 'hello, good thanks']
newlist=[]
for key, value in word_list.items():
newlist=[x.replace(key,value) for x in sentence]
print newlist
note: updated for python3, thanks @MGM注意:已针对 python3 更新,谢谢@MGM
Using str.replace
is problematic, as a word could be a part of another word.使用str.replace
是有问题的,因为一个词可能是另一个词的一部分。 Better use re.sub
with regular expression \\b\\w+\\b
, with \\b
being "word boundary", and use a callback function to get the replacement from the dictionary (or the word itself, if it is not in the dict).最好将re.sub
与正则表达式\\b\\w+\\b
一起使用, \\b
是“单词边界”,并使用回调函数从字典中获取替换(或单词本身,如果它不在字典中) .
>>> word_list = { "hello" : "1", "bye" : "2", 'you': "3"}
>>> sentence= ['hello you, hows things', 'you is not yourself', 'hello, good thanks']
>>> [re.sub(r'\b\w+\b', lambda m: word_list.get(m.group(), m.group()), s) for s in sentence]
['1 3, hows things', '3 is not yourself', '1, good thanks']
Also, not that by assigning to i
within the loop, you are only changing the value bound to the variable i
;此外,不是通过在循环内分配给i
,您只是更改绑定到变量i
的值; you are not changing the string in the list!您没有更改列表中的字符串! For this, you have to assign to the list element at that index, or use a list comprehension, as in my example.为此,您必须分配给该索引处的列表元素,或使用列表理解,如我的示例所示。
I ran your code verbatim and it worked, but only prints the last "i".我逐字运行了你的代码并且它工作了,但只打印了最后一个“i”。 If you move the print into the for loop you get what you expected.如果您将打印移动到 for 循环中,您会得到预期的结果。
word_list = { "hello" : "1"}
sentence= ['hello you, hows things', 'hello, good thanks']
for key, value in word_list.iteritems():
for i in sentence:
i = i.replace(key, value)
print i
output:输出:
1 you, hows things
1, good thanks
Using regex:使用正则表达式:
>>> word_list = { "hello" : "1", "bye" : "2"}
>>> sentence= ['hello you, hows things', 'hello, good thanks']
>>> [re.sub('|'.join(word_list.keys()), lambda x: word_list[x.group()], i) for i in sentence]
['1 you, hows things', '1, good thanks']
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.