Replacing list item with contents of another list

后端 未结 5 1624
日久生厌
日久生厌 2020-12-21 16:25

Similar to this question, but instead of replacing one item with another, I\'d like to replace any occurrences of one item with the contents of a list.

orig          


        
5条回答
  •  孤城傲影
    2020-12-21 16:49

    No need for anything fancy:

    desired = orig[:2] + repl + orig[3:]
    

    To find 2 you can search for orig.index('c').

    x = orig.index('c')
    desired = orig[:x] + repl + orig[x+1:]
    

    if repl is not a list, just use list(repl)

提交回复
热议问题