社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

比较python中字典列表中的值

AlphabetSoup • 3 年前 • 1559 次点击  

我有一组两种模式的字典,比如 {"Id": 1, "title":"example"} {"Id": 1, "location":"city"} .我想把这两个结合起来 {"Id": 1, "title":"example", "location":"city"} ,适用于所有带有 Id 这是一场比赛。在这种情况下,该组由100个项目中的200个项目组成 title s和100 location 一切都好吗 身份证件 从0到99。我想退回100本合并字典的清单。

可能如下所示:

def ResultHandler(extractedResult: list):
    jsonObj = {}
    jsonList = []
    for result in extractedResult:
        for key, val in result.items():
            #this works if its hardcoded val to a number...
            if key == "Id" and val == 1:
                jsonObj.update(result)
    jsonList.append(jsonObj)
    return jsonList
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130562
 
1559 次点击  
文章 [ 5 ]  |  最新文章 3 年前
Pradip
Reply   •   1 楼
Pradip    3 年前

此代码将在线性时间内解决您的问题,即O(n),其中n是字典长度的增长顺序。只考虑那些 Id 两者兼而有之 title location 并将忽略其余部分。

from collections import Counter

data = [{"Id": 1, "title":"example1"},
        {"Id": 2, "title":"example2"},
        {"Id": 3, "title":"example3"},
        {"Id": 4, "title":"example4"},
        {"Id": 1, "location":"city1"},
        {"Id": 2, "location":"city2"},
        {"Id": 4, "location":"city4"},
        {"Id": 5, "location":"city5"}]

paired_ids = set([key for key, val in dict(Counter([item["Id"] for item in data])).items() if val == 2]) # O(n)

def combine_dict(data):
    result = {key: [] for key in paired_ids} # O(m), m: number of paired ids (m <= n/2)
    for item in data: # O(n)
        items = list(item.items())
        id, tl, val = items[0][1], items[1][0], items[1][1]

        if id in paired_ids: # O(1), as paired_ids is a set lookup takes O(1)
            result[id].append({tl: val})

    return [{"Id": id, "title": lst[0]["title"], "location": lst[1]["location"]} for id, lst in result.items()] # O(n)


print(*combine_dict(data), sep="\n")

输出:

{'Id': 1, 'title': 'example1', 'location': 'city1'}
{'Id': 2, 'title': 'example2', 'location': 'city2'}
{'Id': 4, 'title': 'example4', 'location': 'city4'}
Ani M
Reply   •   2 楼
Ani M    3 年前

以下代码应该可以工作:

def resultHandler(extractedResult):
  jsonList = []
  for i in range(len(extractedResult) // 2):
    jsonList.append({"Id": i})
  for i in range(len(extractedResult)):
    for j in range(len(jsonList)):
      if jsonList[j]["Id"] == extractedResult[i]["Id"]:
        if "title" in extractedResult[i]:
          jsonList[j]["title"] = extractedResult[i]["title"];
        else:
          jsonList[j]["location"] = extractedResult[i]["location"];
  return jsonList;

extractedResult = [{"Id": 0, "title":"example1"}, {"Id": 1, "title":"example2"}, {"Id": 0, "location":"example3"}, {"Id": 1, "location":"example4"}]

jsonList = resultHandler(extractedResult)

print(jsonList)

输出:

[{'Id': 0, 'title': 'example1', 'location': 'example3'}, {'Id': 1, 'title': 'example2', 'location': 'example4'}]

此代码首先填充 jsonList Id值从0到长度的一半 extractedResult (ID的数量也是如此)。

然后,对于每一本字典 提取结果 ,我们在字典里找到了 jsonList 和匹配的ID。如果 提取结果 包含一个密钥, "title" ,然后我们在 jsonList .这同样适用于 "location" .

我希望这有助于回答你的问题!如果您需要任何进一步的澄清或详细信息,请告诉我:)

Parvesh Kumar
Reply   •   3 楼
Parvesh Kumar    3 年前

此函数有一个嵌套循环。外部循环遍历字典列表。内环 再次遍历字典列表,检查当前字典的id是否正确 已经在字典列表中了。如果不是,它会将字典添加到 字典。如果是,它会使用以下内容更新字典列表中的字典: 当前的字典。

lst = [
    {"id": 1, "fname": "John"},
    {"id": 2, "name": "Bob"},
    {"id": 1, "lname": "Mary"},
]
def combine_dicts(lst):
    res = []
    for d in lst:
        if d.get("id") not in [x.get("id") for x in res]:
            res.append(d)
        else:
            for r in res:
                if r.get("id") == d.get("id"):
                    r.update(d)
    return res


print(combine_dicts(last))
# output: [{'id': 1, 'fname': 'John', 'lname': 'Mary'}, {'id': 2, 'name': 'Bob'}]
ddejohn
Reply   •   4 楼
ddejohn    3 年前

更实用(但效率稍低)的方法:

from itertools import groupby
from functools import reduce
from operator import itemgetter


new_data = []
for _, g in groupby(sorted(data, key=itemgetter("Id")), key=itemgetter("Id")):
    new_data.append(reduce(lambda d1, d2: {**d1, **d2}, g))
FMc
Reply   •   5 楼
FMc    3 年前

按ID对DICT进行分组,然后合并每组。

from collections import defaultdict

def merge_dicts(dicts):
    grouped = defaultdict(list)
    for d in dicts:
        grouped[d['id']].append(d)

    merged = []
    for ds in grouped.values():
        m = {}
        for d in ds:
            m |= d        # If Python < 3.9 : m = {**m, **d}
        merged.append(m)

    return merged