β

Pythonic way to convert a list into dictionary

life is short - you need Python! 241 阅读
Here is a simple Python code that converts a list into a dictionary.

 def list_to_dict(li):  
     dct = {}  
     for item in li:  
         if dct.has_key(item):  
             dct[item] = dct[item] + 1  
         else:  
             dct[item] = 1  
     return dct  
 li = [1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4, 5, 6, 7, 7]  
 print list_to_dict(li)  

Now I am looking for more Pythonic way to do this task. Any ideas?
作者:life is short - you need Python!
A blog on Python with tutorials, code, programs, tips and tricks, how-to, book-list, crawler / spide
原文地址:Pythonic way to convert a list into dictionary, 感谢原作者分享。