合并字典项值的重复列表 [英] Merge duplicates list of dictionaries item value
问题描述
我有以下列出重复的字典(值)(教练).
I have following list of dictionaries with a duplication in value (coach).
d = [
{"players": [{"id": 179, "name": "Santosh"}], "coach": "Xavi Alonso"},
{"players": [{"id": 180, "name": "Hari"}], "coach": "Xavi Alonso"},
{"players": [{"id": 175, "name": "Hitman"}], "coach": "Anna Puyol"}
]
我想根据教练姓名合并重复项.我该如何实现?
I want to merge duplicates items based on coach name. How can i achieve this?
def exportplayers(request):
d = [
{"players": [{"id": 179, "name": "Santosh"}, {"id": 180, "name": "Hari"}], "coach": "Xavi Alonso"},
{"players": [{"id": 175, "name": "Hitman"}], "coach": "Anna Puyol"}
]
return HttpResponse(json.dumps(d))
推荐答案
您可以使用 groupby
(来自itertools模块)和 operator
模块中的itemgetter
.将 grouby 操作视为从记录中返回连续键和组的一种方式.为了使其正常工作,您需要根据sorted
函数在此处执行的"coach"值对列表中的"items"进行排序.当然,您需要提供一个关键功能来自定义排序顺序,而实现此目的的一种方法是使用像sorted(d, key=lambda _: _["coach"])
这样的lambda表达式,但我更喜欢使用sorted(d, key=itemgetter('coach'))
的另一种方法.需要将groupby
的key参数提供给key=itemgetter("coach")
.
You can do this using groupby
from the itertools module and itemgetter
from the operator
module. Think of the grouby operation as a way to return a consecutive keys and groups from your record. In order for it to work you need to your "items" in the list to be sorted by "coach" value that is what the sorted
function does here. Of course you need to supply a key function in order to customize your sort order and one way to do this is using a lambda expression like this sorted(d, key=lambda _: _["coach"])
but I prefer the alternative approach which is using sorted(d, key=itemgetter('coach'))
.That being said you also need to supply the key argument to groupby
which is a function computing a key value for each element as mentioned in the documentation, and again you can use key=itemgetter("coach")
.
from itertools import groupby
from operator import itemgetter
def exportplayers(request):
d = [
{"players": [{"id": 179, "name": "Santosh"}, {"id": 180, "name": "Hari"}], "coach": "Xavi Alonso"},
{"players": [{"id": 175, "name": "Hitman"}], "coach": "Anna Puyol"}
]
my_list = []
for g, data in groupby(sorted(d, key=itemgetter('coach')), key=itemgetter('coach')):
my_list.append({"coach": g, "players": [player for item in data for player in item["players"]]})
return HttpResponse(json.dumps(my_list))
使用lambda表达式作为键函数的演示:
Demo using lambda expressions as key function:
>>> from itertools import groupby
>>> d = [
... {"players": [{"id": 179, "name": "Santosh"}], "coach": "Xavi Alonso"},
... {"players": [{"id": 180, "name": "Hari"}], "coach": "Xavi Alonso"},
... {"players": [{"id": 175, "name": "Hitman"}], "coach": "Anna Puyol"}
... ]
>>> sorted_d = sorted(d, key=lambda _: _['coach'])
>>> for g, data in groupby(sorted_d, key=lambda _: _['coach']):
... print({"coach": g, "players": [player for item in data for player in item["players"]]})
...
{'coach': 'Anna Puyol', 'players': [{'id': 175, 'name': 'Hitman'}]}
{'coach': 'Xavi Alonso', 'players': [{'id': 179, 'name': 'Santosh'}, {'id': 180, 'name': 'Hari'}]}
这篇关于合并字典项值的重复列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!