我有下列词典清单
dataset={"users": [
{"id": 20, "loc": "Chicago", "st":"4", "sectors": [{"sname": "Retail"}, {"sname": "Manufacturing"}, {"sname": null}]},
{"id": 21, "loc": "Frankfurt", "st":"4", "sectors": [{"sname": null}]},
{"id": 22, "loc": "Berlin", "st":"6", "sectors": [{"sname": "Manufacturing"}, {"sname": "Banking"},{"sname": "Agri"}]},
{"id": 23, "loc": "Chicago", "st":"2", "sectors": [{"sname": "Banking"}, {"sname": "Agri"}]},
{"id": 24, "loc": "Bern", "st":"1", "sectors": [{"sname": "Retail"}, {"sname": "Agri"}]},
{"id": 25, "loc": "Bern", "st":"4", "sectors": [{"sname": "Retail"}, {"sname": "Agri"}, {"sname": "Banking"}]}
]}
我试着用下面的代码从上面的列表中删除loc,扇区 这样我的列表就只包含id和loc了
fs_loc = []
for g, items in itertools.groupby(data['users'], lambda x: (x['id'],x['loc'])):
fs_loc.append({ 'id': g[0], 'loc': g[1] })
print(fs_loc)
从这里,我如何才能创建新的列表,这样它将有一个id的列表和他们的计数,如下面的位置分组。你知道吗
{"locations": [
{"loc": "Chicago","count":2,"ids": [{"id": "20"}, {"id": "23"}]},
{"loc": "Bern","count":2,"ids": [{"id": "24"}, {"id": "25"}]},
{"loc": "Frankfurt","count":1,"ids": [{"id": "21"}]},
{"loc": "Berlin","count":1,"ids": [{"id": "21"}]}
]}
我发现使用itertools制作上述列表有困难,可能我缺少一些更好的方法来实现上述目标,请您提出建议。你知道吗
您需要将排序后的序列传递给
itertools.groupby
。你知道吗根据^{} documentation :
fs_loc
→相关问题 更多 >
编程相关推荐