on-internet
← Community

How to recode integers to other values in python?

Stack Overflow ·

I was surprised this question has not been asked yet. Given an array of integers x = np.random.randint(1,4,10) > array([3, 2, 1, 2, 1, 1, 3, 3, 2, 1]) How can I (easily) change all values according to some systematic rule, such as x[x==1] = 5 x[x==2] = 6 x[x==3] = 7 > array([6, 5, 6, 7, 5, 5, 6, 5, 7, 6]) The rule may be more complex than this, e.g. recode 1=2 and 2=1 so that the order plays a role.

Comments

Sign in or sign up to comment and upvote.

No comments yet.