Python 按键值筛选字典列表-将答案作为字典列表返回
我有一个要筛选的词典列表Python 按键值筛选字典列表-将答案作为字典列表返回,python,list,dictionary,filter,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}] 我的目标是得到两本字典的列表。一个具
[{"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}]
您可以将a传递到max()
:
但在pythonland中,这可能只有在您的输入非常大和笨拙时才有价值
请注意,在这两种情况下,\ u对max\u pos
/max\u neg
的进一步修改构成对行的成员的修改,因为这些成员是可变字典。这行吗
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)
您可以通过以下方式执行此操作:
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'])
你想要最大值和最小值。。使用它们并应用适当的键函数-事实上,使用元组,您只需要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)
元组按第一个值排序,然后按第二个值排序-您可以构建元组并将其用作最大键函数。创建一个负斜率的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]]
要在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}]
或者,如果您喜欢列表形式:
[x[-1] for x in (negatives, positives)]
# [{'Slope': -0.562, 'Count': 12}, {'Slope': 2.5, 'Count': 34}]
@这将需要更少的计算-同意-但排序将把负斜率和最少的计数和背面-不完全是需要的吗?它的工作原理惊人!!但是……事实证明,我可能并不总是有一对积极和消极的观点。你知道我如何将每个正斜率和负斜率值排序到一个有序列表中(按“计数”)?然后我可以检查两者的长度,然后添加一些额外的条件?@Lew您可能想使用BradSolomons或Vasilis方法:列表理解:a,b=排序((如果I[“Slope”]<0,则数据中的I表示I),key=lambda x:x[“Count”]),sorted((如果I[“Slope”]>=0,则数据中的I表示I),key=lambda x:x[“Count”])
或使用过滤器()
功能执行相同操作。不过,在这两种情况中的一种情况下,您应该包括“Slope”==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}]
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}]
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]]
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}]