2013-04-05 280 views
1

我有一個幾乎讓我發瘋的問題。Python將元組列表轉換爲嵌套字典列表

我在Python編程,我有一個元組列表:

my_tuples = [ 
    ('csrf_token', u'5bc7224ef8f71c8142b4bef1597883b049b83fee'), 
    ('fields-team', u'dsada'), 
    ('fields-year_from', u'dsa'), 
    ('fields-year_to', u'dsa'), 
    ('fields-description', u'dsa') , 
    ('fields-team', u''), 
    ('fields-year_from', u''), 
    ('fields-year_to', u''), 
    ('fields-description', u'') 
] 

我不得不元組的這個名單變成這樣:

form_data = { 
    'csrf_token':u'5bc7224ef8f71c8142b4bef1597883b049b83fee', 
    'fields':[ 
     { 
      'team': u'dsada', 
      'year_from': u'dsa', 
      'year_to': u'dsa', 
      'description': u'dsa' 
     }, 
     { 
      'team': u'', 
      'year_from': u'', 
      'year_to': u'', 
      'description': u'' 
     } 
    ] 
} 

然後,我不得不刪除第二個字典,因爲每個鍵都有一個空值。 所以最終的結果將是:

form_data = { 
    'csrf_token':u'5bc7224ef8f71c8142b4bef1597883b049b83fee', 
    'fields':[ 
     { 
      'team': u'dsada', 
      'year_from': u'dsa', 
      'year_to': u'dsa', 
      'description': u'dsa' 
     } 
    ] 
} 
+1

將第二字典什麼樣子好像是 「減肥」? – Gerrat 2013-04-05 17:57:37

+3

確保這4個「字段」會出現嗎?是否有可能出現您未顯示的其他字段? – jedwards 2013-04-05 17:59:33

+0

是的也有可能,這些字段有不同的名字... – Michael 2013-04-05 18:02:13

回答

0

如何:

def myfunc(my_tuples): 
    mydict = {} 
    d = {} 
    for kv in my_tuples: 
     if kv[0] == 'csrf_token': 
      mydict[kv[0]] = kv[1].decode('ascii') 
     elif 'fields' in kv[0]: 
      d[kv[0]] = kv[1].decode('ascii') 
    mydict['fields'] = [d] 
    return mydict 


def main(): 
    my_tuples = [ 
     ('csrf_token', u'5bc7224ef8f71c8142b4bef1597883b049b83fee'), 
     ('fields-team', u'dsada'), 
     ('fields-year_from', u'dsa'), 
     ('fields-year_to', u'dsa'), 
     ('fields-description', u'dsa'), 
     ('fields-team', u''), 
     ('fields-year_from', u''), 
     ('fields-year_to', u''), 
     ('fields-description', u'') 
    ] 

    myfunc(my_tuples) 


if __name__ == "__main__": 
    main() 
2

我的兩分錢:

import collections 

def transform(loft): 
    form_data = collections.defaultdict(list) 
    internal = {} 
    numfield = 0 
    for t in my_tuples: 
     keys = t[0].split("-") 
     if len(keys) == 1: 
      form_data[keys[0]] = t[1] 
     else: 
      internal[keys[1]] = t[1] 
      if numfield == 3: 
       numfield = -1 
       form_data[keys[0]].append(internal) 
       internal = {} 
      numfield += 1 
    return form_data 

if __name__ == "__main__": 

    my_tuples = [ 
     ('csrf_token', u'5bc7224ef8f71c8142b4bef1597883b049b83fee'), 
     ('fields-team', u'dsada'), 
     ('fields-year_from', u'dsa'), 
     ('fields-year_to', u'dsa'), 
     ('fields-description', u'dsa') , 
     ('fields-team', u''), 
     ('fields-year_from', u''), 
     ('fields-year_to', u''), 
     ('fields-description', u'') 
    ] 
    trans = transform(my_tuples) 
    # cleanup 
    trans['fields'] = [f for i, f in enumerate(trans['fields']) 
     if trans['fields'][i]['team'] != '' 
     and trans['fields'][i]['year_from'] != '' 
     and trans['fields'][i]['year_to'] != '' 
     and trans['fields'][i]['description'] != ''] 
    print dict(trans)