私はstring.replace forループを使用したいと思います。これは私のコードです:Python forループでString.Replaceを使用してください。
new = ['p','q','r']
my_str = 'there are two much person a, person b, person c.'
old = ['a','b','c']
for i in range(0,len(old)):
my_str = string.replace(my_str,old[i],new[i])
print(my_str)
しかし、私にエラー与えている:
TypeError: 'str' object cannot be interpreted as an integer
所望の出力:これは単なる一例である
there are two much person p, person q, person r.
を、私はのためのforループを実行したいです10,000の長さのリスト。
there are two much person p, person q, person r.
を再現することはできません。あなたのコードはPython 2.7でうまく動作します –'string.replace'関数は非常に長い間廃止されました。代わりに'str.replace'メソッドを使うべきです。 –
あなたが知っている奇妙な問題を修正しても、あなたはまだ 'are 'の' a'と 'much'の' c'を置き換えるつもりです。 – user2357112