Python Map List of Strings to Integer List

后端 未结 9 1234
猫巷女王i
猫巷女王i 2020-12-31 23:05

Let\'s say I have a list

l = [\'michael\',\'michael\',\'alice\',\'carter\']

I want to map it to the following:

k = [1,1,2,3         


        
9条回答
  •  别那么骄傲
    2020-12-31 23:44

    How about using Pandas?

    import pandas as pd
    l = ['michael','michael','alice','carter']
    pd.Series(l).astype('category').cat.codes.values
    

提交回复
热议问题