一个字典的dictlike字符串列表

2024-04-25 12:14:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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'}

Tags: fordatastyletypebuttonlengthlongfit