finding and replacing elements in a list

前端 未结 16 2464
南方客
南方客 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:48

    >>> a= [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1]
    >>> for n, i in enumerate(a):
    ...   if i == 1:
    ...      a[n] = 10
    ...
    >>> a
    [10, 2, 3, 4, 5, 10, 2, 3, 4, 5, 10]
    

提交回复
热议问题