如何在python django中制作自定义数组

时间:2012-12-03 01:50:28

标签: python django

我有这个dict列表

{"FieldFlags": "0", "FieldNameAlt": "Please forgive me", "FieldName": "field1", "FieldType": "Text", "FieldJustification": "Left", "FieldValue": "test"},
 {"FieldJustification": "Left", "FieldNameAlt": "Please forgive me", "FieldName": "min_speed", "FieldType": "Text", "FieldFlags": "0"},
 {"FieldJustification": "Left", "FieldNameAlt": "Please forgive me", "FieldName": "avg_speed", "FieldType": "Text", "FieldFlags": "0"}, 
 {"FieldJustification": "Left", "FieldNameAlt": "Please forgive me", "FieldName": "lowest_speed", "FieldType": "Text", "FieldFlags": "0"},
 {"FieldJustification": "Left", "FieldNameAlt": "Please forgive me", "FieldName": "air", "FieldType": "Text", "FieldFlags": "0"}, 
 {"FieldJustification": "Left", "FieldNameAlt": "Please forgive me", "FieldName": "slope", "FieldType": "Text", "FieldFlags": "0"}]

出于我的目的,我需要将此数组转换为类似

的内容
fields = [('field1','test'),('min_speed',''),('avg_speed','')..]

所以基本上我想要(FiedName,FieldValue)

的元组

如果不存在fieldValue,则应显示为空

我如何转换

2 个答案:

答案 0 :(得分:4)

fields = [(d['FieldName'], d.get('FieldValue', '')) for d in your_list]

答案 1 :(得分:0)

您也可以使用map():

fields = map(lambda d: (d['FieldName'], d.get('FieldValue', '')), your_list)