我有一个几乎让我发疯的问题。
我正在用 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'
}
]
}