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
How about using Pandas?
import pandas as pd
l = ['michael','michael','alice','carter']
pd.Series(l).astype('category').cat.codes.values
To map list of integers to list of strings I would use a dictionary, for example:
> name_number = {'michael':1, 'michael':1, 'alice':2, 'carter':3}
> print len(name_number)
3
> print name_number['alice']
2
Note that len(name_number)
is 3
, because duplicate keys are not allowed.
can do it pretty easy without a function :
j - list()
for i in range (len(l)) :
j.append((l[i],k[i]))