简体   繁体   English

比较字典列表中的值并仅返回具有最高值的字典

[英]Compare values in a list of dictionaries and return only dictionaries with highest values

I would like to return a list which contains the dictionary with the highest version number for a given name and given type from dictionaries in a list.我想返回一个列表,其中包含列表中字典中给定名称和给定类型的最高版本号的字典。 I have the following list of dictionaries:我有以下字典列表:

list = [
{
  "name": "name 1",
  "type": "type 1",
  "version": 1,
  }
{ *this one should be returned*
  "name": "name 1",
  "type": "type 1",
  "version": 2,
  }
{
  "name": "name 2",
  "type": "type 1",
  "version": 1,
  }
{ *and this one*
  "name": "name 2",
  "type": "type 1",
  "version": 2,
  }
{
  "name": "name 1",
  "type": "type 2",
  "version": 1,
  }
{ *and this one*
  "name": "name 1",
  "type": "type 2",
  "version": 2,
  }

I would like to be able to return a list just containing the highest version number for a type and a name, so the result for the above would be:我希望能够返回一个仅包含类型和名称的最高版本号的列表,因此上述结果将是:

  returned_list = [
{
  "name": "name 1",
  "type": "type 1",
  "version": 2,
  }
{
  "name": "name 2",
  "type": "type 1",
  "version": 2,
  }
{
  "name": "name 1",
  "type": "type 2",
  "version": 2,
  }

I am not sure where to start, any advice would be appreciated.我不知道从哪里开始,任何建议将不胜感激。

Here is an approach which uses a defaultdict to first assemble a dictionary keyed by name,type pairs and whose values are a list of version numbers, which it then assembles into the desired list:这是一种使用defaultdict的方法,它首先组装以名称、类型对为键的字典,其值是版本号列表,然后将其组装成所需的列表:

from collections import defaultdict

accumulate = defaultdict(list)
for d in my_list:
    accumulate[(d["name"],d["type"])].append(d["version"])

new_list = [{"name":n,"type":t,"version":max(v)} for (n,t),v in accumulate.items()]

result:结果:

>>> for d in new_list:print(d)

{'name': 'name 1', 'type': 'type 1', 'version': 2}
{'name': 'name 2', 'type': 'type 1', 'version': 2}
{'name': 'name 1', 'type': 'type 2', 'version': 2}

Here's one approach这是一种方法

First find the max from the input list.首先从输入列表中找到最大值。 We use max() for this我们为此使用max()

Then find any & all entries from the original list that may have the same "version".然后从原始列表中找到可能具有相同“版本”的所有条目。 We use filter for this我们为此使用filter

name_list = [
    {
        "name": "name 1",
        "type": "type 1",
        "version": 1,
    },
    {
        "name": "name 1",
        "type": "type 1",
        "version": 2,
    },
    {
        "name": "name 2",
        "type": "type 1",
        "version": 1,
    },
    {
        "name": "name 2",
        "type": "type 1",
        "version": 2,
    },
    {
        "name": "name 1",
        "type": "type 2",
        "version": 1,
    },
    {
        "name": "name 1",
        "type": "type 2",
        "version": 2,
    }]

max_in_list = max(name_list, key=lambda x: x['version']) # max by version

maxes_in_list = filter(lambda x: x['version'] == max_in_list['version'], name_list) # select if version == max_version

print(list(maxes_in_list))

Output: Output:

[{'name': 'name 1', 'type': 'type 1', 'version': 2}, {'name': 'name 2', 'type': 'type 1', 'version': 2}, {'name': 'name 1', 'type': 'type 2', 'version': 2}]
lst = []
comp = list_lst[0]['version']
for i in range(len(list_lst)):
    if (list_lst[i]['version']) > comp:
        comp = list_lst[i]['version']
        lst = []
        lst.append(list_lst[i])
    elif (list_lst[i]['version'] == comp):
        lst.append(list_lst[i])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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