Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formation of dictionary from list element

Hi I have list as follows which contain meta data from images as follows:

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

I want to make a dictionary using splitting the list “:” in he following format:

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

Currently I have wrote some code which is not working.

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

This code does not work. I have tried other approaches too, but I am not able to get the format.

like image 825
akira Avatar asked Oct 11 '15 08:10

akira


2 Answers

You can use a list comprehension within a dict comprehension using 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')])
>>> 
like image 137
Mazdak Avatar answered Sep 20 '22 02:09

Mazdak


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

Output:

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

To remove the whitespace you can str.strip it off:

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

Output:

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

Both of which actually match your expected output.

like image 38
Padraic Cunningham Avatar answered Sep 18 '22 02:09

Padraic Cunningham