1

我有一个几乎让我发疯的问题。

我正在用 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'
         }
     ]
 }
4

2 回答 2

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)
于 2013-04-05T20:21:16.603 回答
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()
于 2013-04-05T19:08:32.900 回答