如何过滤字典列表以仅在python中包含几个键?

时间:2018-10-18 18:27:47

标签: python

说我有这个:

[{'dropoff_datetime': '2014-11-26T22:31:00.000',
  'dropoff_latitude': '40.746769999999998',
  'dropoff_longitude': '-73.997450000000001',
  'fare_amount': '52',
  'imp_surcharge': '0',
  'mta_tax': '0.5',
  'passenger_count': '1',
  'payment_type': 'CSH',
  'pickup_datetime': '2014-11-26T21:59:00.000',
  'pickup_latitude': '40.64499',
  'pickup_longitude': '-73.781149999999997',
  'rate_code': '2',
  'tip_amount': '0',
  'tolls_amount': '5.3300000000000001',
  'total_amount': '57.829999999999998',
  'trip_distance': '18.379999999999999',
  'vendor_id': 'VTS'},

{'dropoff_datetime': '2014-02-22T17:54:37.000',
  'dropoff_latitude': '40.781844999999997',
  'dropoff_longitude': '-73.979073',
  'fare_amount': '7.5',
  'imp_surcharge': '0',
  'mta_tax': '0.5',
  'passenger_count': '1',
  'payment_type': 'CSH',
  'pickup_datetime': '2014-02-22T17:47:23.000',
  'pickup_latitude': '40.766931',
  'pickup_longitude': '-73.982097999999993',
  'rate_code': '1',
  'store_and_fwd_flag': 'N',
  'tip_amount': '0',
  'tolls_amount': '0',
  'total_amount': '8',
  'trip_distance': '1.3',
  'vendor_id': 'CMT'}]

我想要这个:

[{'pickup_latitude': '40.64499',
  'pickup_longitude': '-73.781149999999997',
  'trip_distance': '18.379999999999999'}

{'pickup_latitude': '40.766931',
  'pickup_longitude': '-73.982097999999993',
  'trip_distance': '1.3'}]

2 个答案:

答案 0 :(得分:0)

您可以这样做:

tgt={'pickup_latitude',
    'pickup_longitude',
    'trip_distance'}    

>>> [{k:v for k,v in d.items() if k in tgt} for d in list_of_dicts]
[{'pickup_latitude': '40.64499', 'pickup_longitude': '-73.781149999999997', 'trip_distance': '18.379999999999999'}, {'pickup_latitude': '40.766931', 'pickup_longitude': '-73.982097999999993', 'trip_distance': '1.3'}]

答案 1 :(得分:0)

迭代并仅接受所选字段:

detailsList = [{'dropoff_datetime': '2014-11-26T22:31:00.000',
'dropoff_latitude': '40.746769999999998',
'dropoff_longitude': '-73.997450000000001',
'fare_amount': '52',
'imp_surcharge': '0',
'mta_tax': '0.5',
'passenger_count': '1',
'payment_type': 'CSH',
'pickup_datetime': '2014-11-26T21:59:00.000',
'pickup_latitude': '40.64499',
'pickup_longitude': '-73.781149999999997',
'rate_code': '2',
'tip_amount': '0',
'tolls_amount': '5.3300000000000001',
'total_amount': '57.829999999999998',
'trip_distance': '18.379999999999999',
'vendor_id': 'VTS'},

{'dropoff_datetime': '2014-02-22T17:54:37.000',
'dropoff_latitude': '40.781844999999997',
'dropoff_longitude': '-73.979073',
'fare_amount': '7.5',
'imp_surcharge': '0',
'mta_tax': '0.5',
'passenger_count': '1',
'payment_type': 'CSH',
'pickup_datetime': '2014-02-22T17:47:23.000',
'pickup_latitude': '40.766931',
'pickup_longitude': '-73.982097999999993',
'rate_code': '1',
'store_and_fwd_flag': 'N',
'tip_amount': '0',
'tolls_amount': '0',
'total_amount': '8',
'trip_distance': '1.3',
'vendor_id': 'CMT'}]

required = ['pickup_latitude', 'pickup_longitude', 'trip_distance']
filtered = []
for item in detailsList:
    filteredItem = {}
    for field in required:
        filteredItem[field] = item[field]
    filtered.append(filteredItem)

print(filtered)