Python: Convert list to dictionary with indexes as values

后端 未结 5 1970
傲寒
傲寒 2020-12-02 16:08

I am trying to convert the following list:

list = [\'A\',\'B\',\'C\']

To a dictionary like:

dict = {\'A\':0, \'B\':1, \'C\'         


        
5条回答
  •  感动是毒
    2020-12-02 16:51

    You have to convert the unhashable list into a tuple:

    dct = {tuple(key): idx for idx, key in enumerate(lst)}
    

提交回复
热议问题