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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Have a look at
ord, which gives the unicode number for a given character:So you could do
ord(x)-96to converta-zto1-26(careful about upper case, etc).Again, careful about upper case and non-alphabet characters.