Python Map List of Strings to Integer List

后端 未结 9 1272
猫巷女王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:31

    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.

提交回复
热议问题