Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Map List of Strings to Integer List

Tags:

python

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]

Where michael corresponds to 1, alice corresponds to 2 etc. Is there a function in Python to do this easily?

like image 688
Michael Avatar asked Feb 09 '12 06:02

Michael


4 Answers

Have a look at ord, which gives the unicode number for a given character:

>>> letters = ['a','b','c','d','e','f','g']
>>> [ord(x) for x in letters]
[97, 98, 99, 100, 101, 102, 103]

So you could do ord(x)-96 to convert a-z to 1-26 (careful about upper case, etc).

l = ['a','b','a','c']
k = [ord(x)-96 for x in l] # [1,2,1,3]

Again, careful about upper case and non-alphabet characters.

like image 139
mathematical.coffee Avatar answered Oct 20 '22 17:10

mathematical.coffee


In order to answer the edited question, i.e., to map the list of strings to unique integers, one has to first find the unique strings and then do 1-1 mapping of the strings to integers in the original list of strings. For example,

s = ['michael','michael','alice','carter']

then unique strings are {'michael','alice','carter'}. Now, convert these strings to integers by 1-1 mapping like {'michael','alice','carter'} =[1,2,3] using dictionary {'michael':1,'alice':2,'carter':3}. In the third step, loop through the original list of strings; search the string in the dictionary for the corresponding integer and replace the string by that integer.

s=['michael','michael','alice','carter']

mydict={}
i = 0
for item in s:
    if(i>0 and item in mydict):
        continue
    else:    
       i = i+1
       mydict[item] = i

k=[]
for item in s:
    k.append(mydict[item])

Output:

k=[1, 1, 2, 3]
like image 24
CKM Avatar answered Oct 20 '22 18:10

CKM


How about using Pandas?

import pandas as pd
l = ['michael','michael','alice','carter']
pd.Series(l).astype('category').cat.codes.values
like image 30
Omri Avatar answered Oct 20 '22 17:10

Omri


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.

like image 26
stuhpa Avatar answered Oct 20 '22 18:10

stuhpa