【问题标题】:Filter Python list of dictionaries based on key/value criteria根据键/值条件过滤 Python 字典列表
【发布时间】:2020-05-31 21:50:23
【问题描述】:

我有一个需要过滤的字典 python 列表。具体来说,我需要保留原始列表中的所有元素,但要针对特定​​的键/值标准进行过滤。考虑下面的字典。我需要保留所有具有 radius_mean 键值 >= 13 的对象。谢谢!

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','concavity_se':'0.01587','symmetry_se':'0.03003','fractal_dimension_se':'0.006193','radius_worst':'25.38','texture_worst':'17.3' , 'perimeter_worst': '184.6', 'area_worst': '2019', 'smoothness_worst': '0.1622', 'compactness_worst': '0.6656', 'concavity_worst': '0.7119', '凹点_worst': '0.2654', 'symmetry_worst': '0.4601', 'fractal_dimension_worst': '0.1189'}, {'id': '842517', '诊断': 'M', 'radius_mean': '20.57', 'texture_mean': '17.77', “周长平均值”:“132.9” ,'area_mean':'1326','smoothness_mean':'0.08474','compactness_mean':'0.07864','concavity_mean':'0.0869','concavity_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','凹点_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','凹点_worst': '0.186','symmetry_worst':'0.275','fractal_dimension_worst':'0.08902'},{'id':'84300903','诊断':'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','凹点nts_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','凹点_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.44 ','concavity_worst':'0.4504','凹点_worst':'0.243','symmetry_worst':'0.3613','fractal_dimension_worst':'0.08758'},{'id':'84348301','诊断':' 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','凹点_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。 , '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.}' ,{'id':'84358402','诊断':'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'}]

【问题讨论】:

  • 那么你的问题是什么?
  • [x for x in my_list if float(x['radius_mean']) >= 13]
  • 我想为包含 radius_mean 值 >= 13 的字典过滤字典列表。
  • 谢谢米切尔!成功了!

标签: python dictionary key filtering


【解决方案1】:

我相信这将是一种真正的蟒蛇方式:

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

【讨论】:

    【解决方案2】:

    你可以使用

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

    【讨论】:

      【解决方案3】:

      一种过滤方法是通过推导:

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

      【讨论】:

        【解决方案4】:

        您可以在for loop 中使用简单的inline if 过滤列表

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

        【讨论】:

          猜你喜欢
          • 2015-05-17
          • 2020-02-06
          • 1970-01-01
          • 2017-04-22
          • 2020-09-11
          • 1970-01-01
          • 2012-05-14
          • 2019-12-27
          • 2023-01-14
          相关资源
          最近更新 更多