我有一个文本字符串,我想用一个单词替换两个单词。例如。如果单词为artificial intelligence
,我想将其替换为artificial_intelligence
。这需要在200字的列表和大小为5mb的文本文件上完成。
我尝试了string.replace
,但它只适用于一个元素,而不适用于列表。
实施例
文字='人工智能在深度学习的每一种情况下都对我们有用。'
List a : list b
Artificial intelligence: artificial_intelligence
Deep learning: deep_ learning
...
Text.replace('Artificial intelligence','Artificial_intelligence'
)正在发挥作用。
但
For I in range(len(Lista)):
Text=Text.replace(Lista[I],List b[I])
不起作用。
答案 0 :(得分:3)
我建议您使用dict
替换:
text = "Artificial intelligence is useful for us in every situation of deep learning."
replacements = {"Artificial intelligence" : "Artificial_intelligence",
"deep learning" : "deep_learning"}
然后你的方法有效(尽管它区分大小写):
>>> for rep in replacements:
text = text.replace(rep, replacements[rep])
>>> print(text)
Artificial_intelligence is useful for us in every situation of deep_learning.
对于其他方法(如建议的正则表达式方法),请查看SO: Python replace multiple strings。
答案 1 :(得分:1)
由于您的列表条目与字符串之间存在大小写问题,因此您可以使用带有re.sub()
标记的IGNORECASE
函数来获取所需内容:
import re
list_a = ['Artificial intelligence', 'Deep learning']
list_b = ['artificial_intelligence', 'deep_learning']
text = 'Artificial intelligence is useful for us in every situation of deep learning.'
for from_, to in zip(list_a, list_b):
text = re.sub(from_, to, text, flags=re.IGNORECASE)
print(text)
# artificial_intelligence is useful for us in every situation of deep_learning.
注意使用zip()
函数可以同时迭代这两个列表。
另请注意,Christian是对的,dict更适合您的替代数据。之前的代码将是以下完全相同的结果:
import re
subs = {'Artificial intelligence': 'artificial_intelligence',
'Deep learning': 'deep_learning'}
text = 'Artificial intelligence is useful for us in every situation of deep learning.'
for from_, to in subs.items():
text = re.sub(from_, to, text, flags=re.IGNORECASE)
print(text)