finding and replacing elements in a list

前端 未结 16 2663
南方客
南方客 2020-11-22 05:41

I have to search through a list and replace all occurrences of one element with another. So far my attempts in code are getting me nowhere, what is the best way to do this?<

16条回答
  •  天涯浪人
    2020-11-22 05:45

    a = [1,2,3,4,5,1,2,3,4,5,1,12]
    for i in range (len(a)):
        if a[i]==2:
            a[i]=123
    

    You can use a for and or while loop; however if u know the builtin Enumerate function, then it is recommended to use Enumerate.1

提交回复
热议问题