私はあなたの交換のための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.
を(提案regex-のようにアプローチ)、SO: Python replace multiple stringsをご覧ください。
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()
機能ウィッヒの使用ができますあなたはあなたのリストのエントリと、あなたの文字列の間のケースの問題を抱えているので
200単語はあまり見えません。崇高なテキストを使用して試してみましたか? –
実際に私はこのようなファイルを98個持っています。それは長すぎて手動ですべてを更新することはできません:) –
これはちょうど「一回」の置き換えですか?もしそうなら、私はまだ崇高なテキストを使用し、Ctrl + Shift + Fを実行し、すべての出現を置き換えることをお勧めします。 –