Find and replace multiple values in python

前端 未结 6 1215
攒了一身酷
攒了一身酷 2021-02-15 03:59

I want to find and replace multiple values in an 1D array / list with new ones.

In example for a list

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

I woul

6条回答
  •  死守一世寂寞
    2021-02-15 04:47

    Try this for your expected output, works even if elements not in value_old.

    >>>[val_new[val_old.index(i)] if i in val_old else i for i in a]
    [3, 4, 3, 1, 5, 5, 2, 3]
    

提交回复
热议问题