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)
我的预期输出是:
data={'Closure Type':'Button','Fit':'Regular','Neckline':'Collar Neck',
'Sleeve Length':'Long Sleeve','Sleeve Style':'Cuffed Sleeve'}
答案 0 :(得分:6)
在data structures page of python documentation上:“ dict()构造函数直接从键值对序列中构建字典。”
这可行:
dict(i.split(": ") for i in s)