无法在python中对词典列表中的多个元素进行分组

时间:2015-12-14 17:16:26

标签: python

我有以下代码,它根据位置对ID进行分组。我试图在结果(f_loc)中包含st值以及相应的id,但它没有工作

null=''
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"}]}
    ]}

byloc = lambda x: x['loc']

it = (
    (loc, list(user_grp))
    for loc, user_grp in itertools.groupby(
        sorted(dataset['users'], key=byloc), key=byloc
    )
)
fs_loc = [
    {'loc': loc, 'ids': [x['id'] for x in grp], 'count': len(grp)}
    for loc, grp in it
]

print(fs_loc)

以下是我的尝试:

fs_loc = [
    {'loc': loc, 'ids': [x['id'],y['st'] for x,y in grp], 'count': len(grp)}
    for loc, grp in it
]

我也试过

fs_loc = [
    {'loc': loc, 'ids': [x['id','st'] for x in grp], 'count': len(grp)}
    for loc, grp in it
]

fs_loc = [
    {'loc': loc, 'ids': [x[{'id','st'}] for x in grp], 'count': len(grp)}
    for loc, grp in it
]

这些不起作用,我错过了什么?

我想达到如下结果 -

[
{"loc": "Chicago","count":2,"ids": [{"id":"20","st":"4"}, {"id":"23","st":"2"}]}, 
{"loc": "Bern","count":2,"ids": [{"id":"24","st":"1"}, {"id":"25","st":"4"}]}, 
{"loc": "Frankfurt","count":1,"ids": [{"id":"21","st":"4"}]}, 
{"loc": "Berlin","count":1,"ids": [{"id":"21","st":"4"}]}    
]

[
{"loc": "Chicago","count":2,"ids": [{"20","4"}, {"23","2"}]}, 
{"loc": "Bern","count":2,"ids": [{"24","1"}, {"25","4"}]}, 
{"loc": "Frankfurt","count":1,"ids": [{"21","4"}]}, 
{"loc": "Berlin","count":1,"ids": [{"21","4"}]}    
]

你能建议吗?

1 个答案:

答案 0 :(得分:1)

'ids': [x['id'],y['st'] for x,y in grp]

应该是这样的:

'ids': [{'id':x['id'],'st':x['st']} for x in grp]

如果你想要字典结构,或者:

'ids': [[x['id'],x['st']] for x in grp]

如果你想要列表结构。请记住,grp是一个词典列表,因此您无法将其拆分为2个变量。