例如,我如何获取:
list1 = ['ko', 'aapl', 'hon', 'disca']
至:
dict1 = {'ko': {}, 'aapl': {}, 'hon': {}, 'disca': {}}
<小时/>
我找到的方法
dict(list1)
不幸的是这里不起作用并返回以下错误:
ValueError: dictionary update sequence element #1 has length 4; 2 is required
感谢您的帮助!
最佳答案
尝试以下字典迭代:
dict1 = {i:{} for i in list1}
如果您不喜欢字典交互语法,这里是一个简单的循环:
dict1={}
for i in list1:
dict1[i] = {}
关于python - 如何将具有不同长度值的列表转换为字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48938908/