Home > Enterprise >  How to map an unordered gapped list to an orderd list beginnig from 1?
How to map an unordered gapped list to an orderd list beginnig from 1?

Time:05-20

I have a big unorderd lists of numbers which do not start from one and have an arbitrary gap between them. Is there any command or way (inversable) in python that generate another orderd list of numbers starting from one without a gap?

example:

 from this:
[24608  25901   25901   27352   27352   25063   25065   24330   24330   23111   23111   23111   25253   25252   25901   27352   23111]
 to this
[1  2   2   3   3   4   5   6   6   7   8   8   8   9   2   3   8]

CodePudding user response:

You can use pandas.factorize:

a = np.array([24608, 25901, 25901, 27352, 27352, 25063, 25065, 24330, 24330,
              23111, 23111, 23111, 25253, 25252, 25901, 27352, 23111])

import pandas as pd
b = pd.factorize(a)[0] 1

Output:

array([1, 2, 2, 3, 3, 4, 5, 6, 6, 7, 7, 7, 8, 9, 2, 3, 7])
  • Related