我有这个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,则应显示为空
我如何转换
答案 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)