[英]How do I convert a list with values of different lengths into a dictionary?
For example, how do I get from: 例如,我如何得到:
list1 = ['ko', 'aapl', 'hon', 'disca']
to: 至:
dict1 = {'ko': {}, 'aapl': {}, 'hon': {}, 'disca': {}}
The method I've been able to find 我能找到的方法
dict(list1)
unfortunately does not work here and returns the following error: 遗憾的是在这里不起作用并返回以下错误:
ValueError: dictionary update sequence element #1 has length 4; 2 is required
Thank you for your help! 谢谢您的帮助!
Try the following dictionary iteration: 尝试以下字典迭代:
dict1 = {i:{} for i in list1}
if you don't like the dictionary interation syntax, here it is in a plain loop: 如果你不喜欢字典交互语法,这里它是一个简单的循环:
dict1={}
for i in list1:
dict1[i] = {}
dict(zip(list1,[{} for i in range(len(list1))]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.