![](/img/trans.png)
[英]Pythonic way to convert a dictionary into namedtuple or another hashable dict-like?
[英]list of dict-like strings to one dictionary
s=['Closure Type: Button', 'Fit: Regular', 'Neckline: Collar Neck',
'Sleeve Length: Long Sleeve', 'Sleeve Style: Cuffed Sleeve']
您好,我正在嘗試將上面的列表轉換為一本字典
我需要更改的所有字符串的鍵都不是恆定的。
我試過了
data={{k:v} for i in s for k,v in i.split(': ')}
json.loads(s)
但我明白了
ValueError:要解包的值太多(預期為 2)
我預期的 output 是:
data={'Closure Type':'Button','Fit':'Regular','Neckline':'Collar Neck',
'Sleeve Length':'Long Sleeve','Sleeve Style':'Cuffed Sleeve'}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.