简体   繁体   中英

Filter Python list of dictionaries based on key/value criteria

I have a python list of dictionaries that I need to filter. Specifically, I need all elements from the original list retained but filtered for a specific key/value criteria. Consider the dictionary below. I need all objects retained that have the radius_mean key value >= 13. Thanks!

my_list = [{'id': '842302', 'diagnosis': 'M', 'radius_mean': '17.99', 'texture_mean': '10.38', 'perimeter_mean': '122.8', 'area_mean': '1001', 'smoothness_mean': '0.1184', 'compactness_mean': '0.2776', 'concavity_mean': '0.3001', 'concave points_mean': '0.1471', 'symmetry_mean': '0.2419', 'fractal_dimension_mean': '0.07871', 'radius_se': '1.095', 'texture_se': '0.9053', 'perimeter_se': '8.589', 'area_se': '153.4', 'smoothness_se': '0.006399', 'compactness_se': '0.04904', 'concavity_se': '0.05373', 'concave points_se': '0.01587', 'symmetry_se': '0.03003', 'fractal_dimension_se': '0.006193', 'radius_worst': '25.38', 'texture_worst': '17.33', 'perimeter_worst': '184.6', 'area_worst': '2019', 'smoothness_worst': '0.1622', 'compactness_worst': '0.6656', 'concavity_worst': '0.7119', 'concave points_worst': '0.2654', 'symmetry_worst': '0.4601', 'fractal_dimension_worst': '0.1189'}, {'id': '842517', 'diagnosis': 'M', 'radius_mean': '20.57', 'texture_mean': '17.77', 'perimeter_mean': '132.9', ' area_mean': '1326', 'smoothness_mean': '0.08474', 'compactness_mean': '0.07864', 'concavity_mean': '0.0869', 'concave points_mean': '0.07017', 'symmetry_mean': '0.1812', 'fractal_dimension_mean': '0.05667', 'radius_se': '0.5435', 'texture_se': '0.7339', 'perimeter_se': '3.398', 'area_se': '74.08', 'smoothness_se': '0.005225', 'compactness_se': '0.01308', 'concavity_se': '0.0186', 'concave points_se': '0.0134', 'symmetry_se': '0.01389', 'fractal_dimension_se': '0.003532', 'radius_worst': '24.99', 'texture_worst': '23.41', 'perimeter_worst': '158.8', 'area_worst': '1956', 'smoothness_worst': '0.1238', 'compactness_worst': '0.1866', 'concavity_worst': '0.2416', 'concave points_worst': '0.186', 'symmetry_worst': '0.275', 'fractal_dimension_worst': '0.08902'}, {'id': '84300903', 'diagnosis': 'M', 'radius_mean': '19.69', 'texture_mean': '21.25', 'perimeter_mean': '130', 'area_mean': '1203', 'smoothness_mean': '0.1096', 'compactness_mean': '0.1599', 'concavity_mean': '0.1974', 'concave points _mean': '0.1279', 'symmetry_mean': '0.2069', 'fractal_dimension_mean': '0.05999', 'radius_se': '0.7456', 'texture_se': '0.7869', 'perimeter_se': '4.585', 'area_se': '94.03', 'smoothness_se': '0.00615', 'compactness_se': '0.04006', 'concavity_se': '0.03832', 'concave points_se': '0.02058', 'symmetry_se': '0.0225', 'fractal_dimension_se': '0.004571', 'radius_worst': '23.57', 'texture_worst': '25.53', 'perimeter_worst': '152.5', 'area_worst': '1709', 'smoothness_worst': '0.1444', 'compactness_worst': '0.4245', 'concavity_worst': '0.4504', 'concave points_worst': '0.243', 'symmetry_worst': '0.3613', 'fractal_dimension_worst': '0.08758'}, {'id': '84348301', 'diagnosis': 'M', 'radius_mean': '11.42', 'texture_mean': '20.38', 'perimeter_mean': '77.58', 'area_mean': '386.1', 'smoothness_mean': '0.1425', 'compactness_mean': '0.2839', 'concavity_mean': '0.2414', 'concave points_mean': '0.1052', 'symmetry_mean': '0.2597', 'fractal_dimension_mean': '0.09744', 'radius_se': '0.4956', 'texture_se': '1 .156', 'perimeter_se': '3.445', 'area_se': '27.23', 'smoothness_se': '0.00911', 'compactness_se': '0.07458', 'concavity_se': '0.05661', 'concave points_se': '0.01867', 'symmetry_se': '0.05963', 'fractal_dimension_se': '0.009208', 'radius_worst': '14.91', 'texture_worst': '26.5', 'perimeter_worst': '98.87', 'area_worst': '567.7', 'smoothness_worst': '0.2098', 'compactness_worst': '0.8663', 'concavity_worst': '0.6869', 'concave points_worst': '0.2575', 'symmetry_worst': '0.6638', 'fractal_dimension_worst': '0.173'}, {'id': '84358402', 'diagnosis': 'M', 'radius_mean': '20.29', 'texture_mean': '14.34', 'perimeter_mean': '135.1', 'area_mean': '1297', 'smoothness_mean': '0.1003', 'compactness_mean': '0.1328', 'concavity_mean': '0.198', 'concave points_mean': '0.1043', 'symmetry_mean': '0.1809', 'fractal_dimension_mean': '0.05883', 'radius_se': '0.7572', 'texture_se': '0.7813', 'perimeter_se': '5.438', 'area_se': '94.44', 'smoothness_se': '0.01149', 'compactness_se': '0.02461', 'concavity_se' : '0.05688', 'concave points_se': '0.01885', 'symmetry_se': '0.01756', 'fractal_dimension_se': '0.005115', 'radius_worst': '22.54', 'texture_worst': '16.67', 'perimeter_worst': '152.2', 'area_worst': '1575', 'smoothness_worst': '0.1374', 'compactness_worst': '0.205', 'concavity_worst': '0.4', 'concave points_worst': '0.1625', 'symmetry_worst': '0.2364', 'fractal_dimension_worst': '0.07678'}]

You can use

rad_gt_13 = [x for x in my_list if float(x["radius_mean"]) >= 13 ]

One way to filter is via a comprehension:

filtered_list = [d for d in my_list if float(d['radius_mean']) >= 13.0]

You can filter the list using a simple inline if inside for loop

filtered_list = [i for i in my_list if float(i['radius_mean']) >= 13.0]

I believe this would be a really pythonic way:

filtered_list = filter(lambda d: float(d['radius_mean']) >= 13.0, my_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM