2015-10-11 50 views
6

您好我有名單如下包含從圖像元數據如下:形成字典從列表元素

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

此代碼無效。我也嘗試了其他方法,但我無法獲得格式。

+0

您是否期待字典的字典列表作爲輸出(如您的第一種情況)? –

+0

@akira,請使用複選標記按鈕接受足夠的答案。這是值得的+2代表給你。 – kdbanman

回答

3

可以使用collections.OrderedDict使用字典理解中的列表理解:

>>> li=['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'] 
>>> d=OrderedDict((sub[0],{sub[1]:sub[2:]}) if sub[2:] else (sub[0],sub[1]) for sub in [item.split(':') for item in li]) 
>>> d 
OrderedDict([('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')]) 
>>> 
1

可以完美地解決使用遞歸拆分限制的問題,(的split第二個參數可以用來限制分割數):

def make_dict(l): 
    d = dict() 
    for elem in l: 
     key, value = elem.split(':', 1) 
     if ':' in value: 
      d[key] = make_dict([value]) 
     else: 
      d[key] = value 
    return d 

和測試都是符合您的期望:

>>> l = ['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'] 
>>> make_dict(l) 
{'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'} 
+0

非常感謝。其實很有幫助 – akira

3
l = ['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'] 

d = {} 

for ele in l: 
    spl = ele.split(":", 2) 
    if len(spl) == 3: 
     k1, k2, v = spl 
     d[k1] = {k2: v.split(",")} 
    else: 
     k,v = spl 
     d[k] = v.split() if "," in v else v 

輸出:

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

刪除空格,您可以str.strip它關閉:

d = {} 

for ele in l: 
    spl = ele.split(":", 2) 
    if len(spl) == 3: 
     k1, k2, v = spl 
     d[k1] = {k2.strip(): list(map(str.strip,v.split(",")))} 
    else: 
     k,v = spl 
     d[k] = list(map(str.strip, v.split())) if "," in v else v.strip 

輸出:

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

這兩者實際上是符合您的預期輸出。

2

如果要處理任何級別的字典嵌套,可以使用下面的遞歸算法。示例 -

def makedict(elem): 
    if ':' in elem: 
     k,v = map(str.strip, elem.split(':',1)) 
     return {k:makedict(v)} 
    elif ',' in elem: 
     elems = list(map(str.strip, elem.split(','))) #Simply map(...) for Python 2.x 
     return elems 
    return elem 

如果你想詞典的詞典,你可以做 -

d = {} 
for elem in s: 
    d.update(makedict(elem)) 

或者,如果你想dictionries的詞典列表呼籲每個元素的上述功能列表列表中的理解,例如 -

result = [makedict(elem) for elem in yourlist] 

演示的字典詞典 -

>>> d = {} 
>>> for elem in s: 
...  d.update(makedict(elem)) 
... 
>>> d 
{'Component 2': {'Cb component': ['Quantization table 1', 'Sampling fac 
>>> import pprint 
>>> pprint.pprint(d) 
{'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'} 
+1

遞歸處理任何級別的字典嵌套 –

+0

我在猜測'{Component 1:{Y component:'意思是字典的字典。 –

+0

當然,我這樣做很簡單,就是將列表編輯的邏輯更改爲創建字典字典的字典,如上例中更新的 –