Iterative find/replace from a list of tuples in Python

前端 未结 5 1615
走了就别回头了
走了就别回头了 2020-12-17 05:37

I have a list of tuples, each containing a find/replace value that I would like to apply to a string. What would be the most efficient way to do so? I will be applying this

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 05:42

    A couple notes:

    1. The boilerplate argument about premature optimization, benchmarking, bottlenecks, 100 is small, etc.
    2. There are cases where the different solutions will return different results. if y = [('one', 'two'), ('two', 'three')] and x = 'one' then mhawke's solution gives you 'two' and Unknown's gives 'three'.
    3. Testing this out in a silly contrived example mhawke's solution was a tiny bit faster. It should be easy to try it with your data though.

提交回复
热议问题