更新字符串替换以忽略列表中的值
我试图替换字符串中的子字符串,条件是被替换的值不在忽略列表中。例如,由于 id_1 在 ignore_list 中,因此不应替换 test_str 中的 'id_1' :
ignore_list = ['id_1']
test_str = "id_1Testid"
test_str = test_str.replace('id' , 'test2')
test_str应该包含“ id_1Testtest2”而不是“ test2_1Testtest2”
如何更新,使得项目在ignore_list其在test_str不更换?