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: too many values to unpack (expected 2)
我的预期产出是:
data={'Closure Type':'Button','Fit':'Regular','Neckline':'Collar Neck',
'Sleeve Length':'Long Sleeve','Sleeve Style':'Cuffed Sleeve'}
在data structures page of python documentation中:“dict()构造函数直接从键值对序列构建字典”。你知道吗
所以这是可行的:
相关问题 更多 >
编程相关推荐