7

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?

10 Answers 10

12

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This only works for a list of characters, not for a list of strings as in the OP's question.
Note that the original question only had a list of characters. The edit to the question was made after this answer was given and accepted!
Ah I didn't notice that. In that case, I wish the OP had asked the 'string' question as a separate question. Thanks.
7

How about using Pandas?

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

Comments

5

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]

Comments

4

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.

Comments

2

If I'm reading you correctly, you want to take a list of characters and convert them to integers, with a being 1, b being 2, etc.

l = ['a','b','a','c']
k = [ord(x.upper()) - 64 for x in l]

Threw the upper() in there so it doesn't matter whether they're upper case or lower.

Comments

1

The function is zip

E.g:

>>> l = ['a','b','a','c']
>>> k = [1,2,1,3]¨
>>> zip(l,k)
[('a', 1), ('b', 2), ('a', 1), ('c', 3)]

If you want to use the items of l as index, you want an dictionary:

>>> d = dict(zip(l,k))
>>> d
{'a': 1, 'c': 3, 'b': 2}
>>> d['a']
1
>>> d['c']
3
>>> 

1 Comment

zip does not check for correspondence, if k is reversed, the results will be different.
0

can do it pretty easy without a function :

j - list()    
for i in range (len(l)) : 
   j.append((l[i],k[i]))

Comments

0

From your question it is not clear if you want to generate k based on l or both l and k are given.

If you are looking to create k based on l, @mathematical.coffee's answer should do.

If you want a map from items in l to k, obviously, your items in l should be unique.

See if this is what you were looking for

dict((l[index], k[index]) for index in range(len(l)))

Or else, if you are looking for tuples:

[(l[index], k[index]) for index in range(len(l))]

Comments

0

If you don't care about the order of the assigned ids, this works:

# create unique list of  names
unique_l = set(l)

# create mappings from names to id
name2id = {name: idx+1 for idx, name in enumerate(unique_l)}

# map initial list of names to ids
k = [name2id[name] for name in l]

Output:

[2, 2, 1, 3]

Comments

0

If you do not mind missing the mapping restriction, probably this is another good may to do it.

import numpy as np
names, tags = np.unique(l, return_inverse=True)

print(names)  # ['alice' 'carter' 'michael']
print(tags)   # [2, 2, 0, 1]

Hope this can help in the future.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.