A better way to rewrite multiple appended replace methods using an input array of strings in python?

前端 未结 2 1772
心在旅途
心在旅途 2021-01-16 17:09

I have a really ugly command where I use many appended \"replace()\" methods to replace/substitute/scrub many different strings from an original string. For example:

<
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-16 18:10

    You have the right idea. Use sequence unpacking to iterate each pair of values:

    def replaceAllSubStrings(originalString, replacementArray):
        for in_rep, out_rep in replacementArray:
            originalString = originalString.replace(in_rep, out_rep)
        return originalString
    

提交回复
热议问题