0

我有这个字典列表

{"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 不存在,那么它应该显示为空

我怎样才能转换它

4

2 回答 2

4
fields = [(d['FieldName'], d.get('FieldValue', '')) for d in your_list]
于 2012-12-03T01:54:33.443 回答
0

您还可以使用 map():

fields = map(lambda d: (d['FieldName'], d.get('FieldValue', '')), your_list)
于 2012-12-03T01:59:31.900 回答