# 查找和replace列表中的元素（python）

``>>> a = [1,2,3,4,5,1,2,3,4,5,1]` `

` `>>> a = [10, 2, 3, 4, 5, 10, 2, 3, 4, 5, 10]` `

` `>>> 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]` `

` `>>> a=[1,2,3,1,3,2,1,1] >>> [4 if x==1 else x for x in a] [4, 2, 3, 4, 3, 2, 4, 4]` `

```     >>> a = [1,2,3,2,3,4,3,5,6,6,5,4,5,4,3,4,3,2,1]
>>> map（lambda x：x if x！= 4 else'sss'，a）
[1,2,3,4,5,5,6,5,5，'sss'，5，'sss'，3，'sss'，3,2,1]
```
` `>>> a=[1,2,3,4,5,1,2,3,4,5,1] >>> item_to_replace = 1 >>> replacement_value = 6 >>> indices_to_replace = [i for i,x in enumerate(a) if x==item_to_replace] >>> indices_to_replace [0, 5, 10] >>> for i in indices_to_replace: ... a[i] = replacement_value ... >>> a [6, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6] >>>` `

` `a = [1, 2, 3, 4, 1, 5, 3, 2, 6, 1, 1] dic = {1:10, 2:20, 3:'foo'} print([dic[n] if n in dic else n for n in a]) > [10, 20, 'foo', 4, 10, 5, 'foo', 20, 6, 10, 10]` `

` ` a = [1,2,3,4,5,1,2,3,4,5,1] #Replacing every 1 with 10 for i in xrange(len(a)): if a[i] == 1: a[i] = 10 print a` `