您好我有名單如下包含從圖像元數據如下:形成字典從列表元素
['Component 1: Y component: Quantization table 0, Sampling factors 1 horiz/1 vert',
'Component 2: Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert',
'Component 3: Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert',
'Compression Type: Progressive, Huffman', 'Content-Length: 14312', 'Content-Type: image/jpeg’]
我想打一個字典使用拆分列表:在他下面的格式「」:
{Component 1: {Y component: [Quantization table 0, Sampling factors 1 horiz/1 vert’],
Component 2: {Cb component: [Quantization table 1, Sampling factors 1 horiz/1 vert]},
Component 3: {Cr component: [Quantization table 1, Sampling factors 1 horiz/1 vert]},
Compression Type: [Progressive, Huffman],Content-Length: 14312,Content-Type: image/jpeg}
目前我寫了一些代碼不工作。
def make_dict(seq):
res = {}
if seq[0] is not '':
for elt in seq:
k, v = elt.split(':')
try:
res[k].append(v)
except KeyError:
res[k] = [v]
print res
此代碼無效。我也嘗試了其他方法,但我無法獲得格式。
您是否期待字典的字典列表作爲輸出(如您的第一種情況)? –
@akira,請使用複選標記按鈕接受足夠的答案。這是值得的+2代表給你。 – kdbanman