Python Map List of Strings to Integer List

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

    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.

提交回复
热议问题