将有序字典的列表转换为嵌套列表 [英] Convert list of ordered dict to nested lists

查看:112
本文介绍了将有序字典的列表转换为嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令字典的列表,其中包含一些重复的Id数据..类似这样的

I've list of ordered dict that includes some duplicate Ids in data.. something like this

[OrderedDict([('caseId', 20), ('userId', 1), ('emailStatus', 21)]), 
 OrderedDict([('caseId', 20), ('userId', 1), ('emailStatus', 20)]), 
 OrderedDict([('caseId', 18), ('userId', 4), ('emailStatus', 21)]), 
 OrderedDict([('caseId', 19), ('userId', 3), ('emailStatus', 21)]), 
 OrderedDict([('caseId', 18), ('userId', 1), ('emailStatus', 20)]),
 OrderedDict([('caseId', 20), ('userId', 3), ('emailStatus', 21)]),
 OrderedDict([('caseId', 18), ('userId', 4), ('emailStatus', 20)]), 
 OrderedDict([('caseId', 19), ('userId', 1), ('emailStatus', 20)])]

我想要一个嵌套列表的列表,像这样;

I want to get a list of nested lists, something like this;

[{
"caseId": "20",
"users": [
  {
    "userId": "1",
    "emailStatus": [
      {
      "emailStatus" : "20"
      },
      {
      "emailStatus" : "21"
      }

    ]
  },
    {
    "userId": "3",
    "emailStatus": [
      {
      "emailStatus" : "21"
      }

    ]

  }
]
},
{
"caseId": "19",
"users": [
  {
    "userId": "1",
    "emailStatus": [
      {
      "emailStatus" : "20"
      }
    ]

  },
    {
    "userId": "3",
    "emailStatus": [
      {
      "emailStatus" : "21"
      }

    ]

  }
]
},
{
"caseId": "18",
"users": [
  {
    "userId": "1",
    "emailStatus": [
      {
      "emailStatus" : "20"
      }
    ]

  },
    {
    "userId": "4",
    "emailStatus": [
      {
      "emailStatus" : "20"
      },
      {
      "emailStatus" : "21"
      }

    ]

  }
]
}
]

呈现这样的嵌套列表;

presenting a nested list like this;

我试图通过遍历两个列表来实现这一点,但是却不知道如何保留上一个和下一个记录以及相同数据的记录..这真令人困惑..如果有人可以给我一个起点,我可以进行遍历我的清单,对您来说非常好.

I tried to achieve this by iterating both lists but couldn't get any idea how to keep record of previous and next records and same data.. that's so confusing.. if anyone can give me a start that how I can iterate my list, it would be very kind of you.

许多问候.

更新的问题

此处有更详细的问题

推荐答案

首先,您可以使用循环和dict.setdefault将数据分组为嵌套字典:

First, you can use a loop and dict.setdefault to group the data in a nested dict:

temp = {}
for d in lst:
    temp.setdefault(d["caseId"], {}).setdefault(d["userId"], []).append(d["emailStatus"])
print(temp)
# {18: {1: [20], 4: [21, 20]}, 19: {1: [20], 3: [21]}, 20: {1: [21, 20], 3: [21]}}

或使用collections.defaultdict:

temp = defaultdict(lambda: defaultdict(list))
for d in lst:
    temp[d["caseId"]][d["userId"]].append(d["emailStatus"])

然后,使用嵌套的混合字典和列表推导来汇总最终结果:

Then, use a nested mixed dict and list comprehension to aggregate your final result:

res = [{"caseId": case, "users": [{"userId": user, "emailStatus": [{"emailStatus": s} for s in status]} 
                                  for user, status in users.items()]} 
       for case, users in temp.items()]
print(res)
# [{'caseId': 18, 'users': [{'userId': 1, 'emailStatus': [{'emailStatus': 20}]}, {'userId': 4, 'emailStatus': [{'emailStatus': 21}, {'emailStatus': 20}]}]},
#  {'caseId': 19, 'users': [{'userId': 1, 'emailStatus': [{'emailStatus': 20}]}, {'userId': 3, 'emailStatus': [{'emailStatus': 21}]}]},
#  {'caseId': 20, 'users': [{'userId': 1, 'emailStatus': [{'emailStatus': 21}, {'emailStatus': 20}]}, {'userId': 3, 'emailStatus': [{'emailStatus': 21}]}]}]

这篇关于将有序字典的列表转换为嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆