【发布时间】:2020-02-09 06:25:43
【问题描述】:
我有一个要过滤的字典列表。
[{"Slope": -0.562, "Count": 3},
{"Slope": -0.362, "Count": 6},
{"Slope": -0.762, "Count": 8},
{"Slope": -0.562, "Count": 12},
{"Slope": 2.5, "Count": 34},
{"Slope": 1.52, "Count": 2},
{"Slope": .56, "Count": 6}]
我的目标是获取两个字典的列表。一个具有“最高计数和正斜率”,另一个具有“最高计数和负斜率”。
我的计划是过滤掉所有正面和负面的,然后对每个列表进行排序,然后使用每个列表的第一条记录创建一个新列表。
排序列表对我来说不是问题,我有这个!
lines_lst.sort(key=lambda i: i['lines_count'])
但是当我尝试这个时过滤似乎不起作用,因为它返回一个字典。
positive_lines = next(item for item in lines_lst if item["Slope"] > 0)
有没有人有一个最终得到以下解决方案?
[{"Slope": -0.562, "Count": 12},{"Slope": 2.5, "Count": 34}]
【问题讨论】:
标签: python list dictionary filter