有这样一组数据:[
{
id:12345,
name:aaa
}
{
id:23456,
name:bbbb
}
......
]有没有什么简便的方法达到这样的目的:按照name的值进行分组,得到这样的结果:[
{
id:12344,
name: aaa
}
{
id:12344,
name: aaa
}
]
[
{
id:12344,
name: bbb
}
{
id:12344,
name: bbb
}
]
2 回答
素胚勾勒不出你
TA贡献1827条经验 获得超9个赞
from itertools import groupby l = [ {"id":1, "name":"aaa"}, {"id":2, "name":"aaa"}, {"id":3, "name":"bbb"}, {"id":4, "name":"bbb"}, ] ret = list()for _, group in groupby(l, lambda x: x.get('name')): ret.append(list(group))print ret
慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
js的写法
$(document).ready(function(){ var l = [ {"id":1, "name":"aaa"}, {"id":2, "name":"aaa"}, {"id":3, "name":"bbb"}, {"id":4, "name":"bbb"}, ]; var aList = _.groupBy(l, "name"); console.log(aList); });
- 2 回答
- 0 关注
- 711 浏览
添加回答
举报
0/150
提交
取消