简体   繁体   English

如何合并列表中具有相等值的字典并连接不相等的值并将其他字段保留在字典中?

[英]How do I merge dictionaries in list that have equal value and concate values that are not equal and keep other field in dict?

there is list of dictionaries, when id in two or more dict are equal,names are equal too,but codes are different.有字典列表,当两个或多个字典中的id相等时,名称也相等,但代码不同。

[
    {
        "id" : 2.0,
        "name":"x",
        "code" : "12345"
    },
    {
        "id" : 2.0,
        "name":"x",
        "code" : "23456"
    },
    {
        "id" : 4.0,
        "name":"y",
        "code" : "6767"
    },
    {
        "id" : 5.0,
        "name":"z",
        "code" : "567"
    },
    {
        "id" : 4.0,
        "name":"y",
        "code" : "55657"
    }
]

I want to merge dict that have common id, then i want to have this list as you can see:我想合并具有公共 ID 的 dict,然后我想拥有这个列表,如您所见:

[
    {
        "id" : 2.0,
        "name":"x",
        "code" : "12345,23456"
    },
    {
        "id" : 4.0,
        "name":"y",
        "code" : "6767,55657"
    },
    {
        "id" : 5.0,
        "name":"z",
        "code" : "567"
    }
]

You can do this using the library pandas .您可以使用库pandas执行此操作。

import pandas as pd
# Here, L1 is your former list
L2 = pd.DataFrame(L1).groupby(["id", "name"], as_index=False).agg({"code": lambda x: ','.join(x.tolist())}).to_json(orient="records")
print(L2)

Output输出

[
  {
    "id": 2.0,
    "name": "x",
    "code": "12345,23456"
  },
  {
    "id": 4.0,
    "name": "y",
    "code": "6767,55657"
  },
  {
    "id": 5.0,
    "name": "z",
    "code": "567"
  }
]

EDIT: Fixed List to String编辑:固定列表到字符串

You should loop through the dictionary like this:你应该像这样循环字典:

dictionary_aux = []
for elem in dictionary:
    found = False
    for new_elem in dictionary_aux:
        if new_elem.id == elem.id:
             new_elem.code = new_elem.code + "," elem.code
             found = True
             break
    if not found:
         dictionary_aux.append(elem)

You have the result in dictionary_aux.您在 dictionary_aux 中有结果。 I hope it helps.我希望它有帮助。

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

相关问题 如何在字典列表中合并python dict的值 - how to merge values of python dict in a list of dictionaries 如何遍历值对列表以将各自的值设置为等于新值列表? - How do I iterate over a list of value pairs to set the respective value equal to a list of new values? 如何在涉及等长字典列表的算术表达式中应用字典值? - How do you apply dictionary values in arithmetic expressions involving a list of equal-length dictionaries? 如何检查字典列表中键的值是否都等于 0 - How check if values of keys in list of dictionaries are both equal 0 如何检查列表/元组中的某些值是否相等? - How do I check if some values in list/tuple is equal? 如何将列表值与不完全相等的数据框列进行比较? - How do I compare list values to a dataframe column that are not exactly equal? 我如何知道 python 的两个或多个字典中是否有任何“相等的值”? - How do I know if there are any 'equal values' in two or more dictionaries in python? 如果键相等,则合并两个词典 - Merge two dictionaries if the keys are equal 从字典列表返回值列表,其中键等于某个值 - Returning a list of values from a list of dictionaries where keys equal a certain value 如何按相同字段合并字典列表并在此过程中对另一个字段求和? - How do I merge a list of dictionaries by an identical field and sum another field in the process?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM