我怎样才能做到这一点?

时间:2011-04-06 12:03:34

标签: python string

l = [u'Dav watson', u'Davis']

结果应该是:

[
    {
        first_name: 'Dav',
        last_name: 'Watson'
    }, {
        firstname:'Davis'
    }
]

1 个答案:

答案 0 :(得分:5)

import string
l = [u'Dav watson', u'Davis']

[dict(zip(('first_name', 'last_name'), map(string.capitalize, l[0].split()))),
 {'firstname': l[1]}]

# returns:

[{'first_name': u'Dav', 'last_name': u'Watson'}, {'firstname': u'Davis'}]