![](/img/trans.png)
[英]How do I concisely convert a python dictionary to a list of tuples, given a dictionary in which the values may be different types?
[英]How do I convert a list with values of different lengths into a dictionary?
例如,我如何得到:
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] = {}
dict(zip(list1,[{} for i in range(len(list1))]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.