按键值过滤字典列表-以字典列表的形式返回答案

时间:2019-10-11 17:38:22

标签: python list dictionary filter

我有一个要过滤的词典列表。

[{"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}]

我的目标是获得两个字典的列表。一个具有“最高计数和POSITVE斜率”,而另一个具有“最高计数和负斜率”。

我的计划是过滤掉所有正面和负面的内容,然后对每个列表进行排序,然后使用每个列表的第一条记录创建一个新列表。

排序列表对我来说不是问题,我已经明白了!

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}]

6 个答案:

答案 0 :(得分:6)

您想要max和min ..使用它们并应用合适的键函数-实际上,使用元组只需要max:

data = [{"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}]

m1 = max(data, key= lambda x: (x["Slope"]>0, x["Count"]))
m2 = max(data, key= lambda x: (x["Slope"]<0, x["Count"]))

result = [m1,m2]

print(result)

输出:

[{'Slope': 2.5, 'Count': 34}, {'Slope': -0.562, 'Count': 12}]

Tuples按第一个值排序,然后按第二个值排序-您可以构建元组并将其用作最大键函数。

答案 1 :(得分:2)

您可以将generator expression传递给((?<=\+)-?)?([.\d]+)

max()

已批准,此解决方案迭代>>> max((d for d in lines_lst if d["Slope"] > 0), key=lambda d: d["Count"]) {'Slope': 2.5, 'Count': 34} >>> max((d for d in lines_lst if d["Slope"] < 0), key=lambda d: d["Count"]) {'Slope': -0.562, 'Count': 12} 两次。如果您有非常大的输入,可以贪婪地遍历一次,跟踪运行的最大/最小:

lines_lst

但是在Python领域,这可能仅在您的输入量很大且笨拙时才有价值。

请注意,在这两种情况下,对import sys max_pos, max_neg = {"Count": -sys.maxsize}, {"Count": -sys.maxsize} for d in lines_lst: ct = d["Count"] if d["Slope"] > 0 and ct > max_pos["Count"]: max_pos = d elif d["Slope"] < 0 and ct > max_neg["Count"]: max_neg = d / max_pos的进一步修改构成对max_neg成员的修改,因为这些成员是可变的字典。

答案 2 :(得分:1)

您可以通过以下方式进行操作:

inList = [{"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}]

maximum = max(filter(lambda elem: elem['Slope'] > 0, inList), key=lambda e: e['Count'])
minimum = max(filter(lambda elem: elem['Slope'] < 0, inList), key=lambda e: e['Count'])

这将返回:

{'Slope': 2.5, 'Count': 34}
{'Slope': -0.562, 'Count': 12}

答案 3 :(得分:1)

创建一个Count2键,该键对于负斜率为负。然后按Count2排序,并获取第一个和最后一个元素。

lines_lst = [{"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}]


for i in range(len(lines_lst)):
    lines_lst[i]['Count2'] = lines_lst[i]['Count']*lines_list[i]['Slope']/abs(lines_list[i]['Slope'])

lines_lst.sort(key=lambda i: i['Count2'])

[lines_lst[0], lines_lst[-1]]

答案 4 :(得分:1)

要在Patrick的答案中解决您的其他信息,您可以执行以下操作以使负面/正面清单排名第一:

positives = sorted((v for v in data if v['Slope'] >= 0), key=lambda x: x['Count'])
negatives = sorted((v for v in data if v['Slope'] < 0), key=lambda x: x['Count'])

# positives:
# [{'Slope': 1.52, 'Count': 2}, {'Slope': 0.56, 'Count': 6}, {'Slope': 2.5, 'Count': 34}]

# negatives:
# [{'Slope': -0.562, 'Count': 3}, {'Slope': -0.362, 'Count': 6}, {'Slope': -0.762, 'Count': 8}, {'Slope': -0.562, 'Count': 12}]

此时获取最大值很简单。只需获取最后一个元素:

max_pox = positives[-1]     # {'Slope': 2.5, 'Count': 34}
max_neg = negatives[-1]     # {'Slope': -0.562, 'Count': 12}

或者,如果您希望以列表形式:

[x[-1] for x in (negatives, positives)]

# [{'Slope': -0.562, 'Count': 12}, {'Slope': 2.5, 'Count': 34}]

答案 5 :(得分:0)

这行吗?

data = [{"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}]
positive_lines = []
negative_lines = []
for i in range(len(data)):
    if data[i]["Slope"] < 0:
        negative_lines.append(data[i])
    else:
        positive_lines.append(data[i])
max_counts = []
max_counts.append(max(positive_lines, key=lambda x:x['Count']))
max_counts.append(max(negative_lines, key=lambda x:x['Count']))
print(max_counts)

输出:

[{'Slope': 2.5, 'Count': 34}, {'Slope': -0.562, 'Count': 12}]