需要删除重复键而不删除整个字典

Need to remove duplicate keys without removing entire dictionary

我想从字典中删除重复的键 "John Doe"。

词典

info = [{"author": "John Doe", "book": {"title": "Getting started with Golang", "rating": 4.2, "category": "programming"}},
        {"author": "John Doe", "book": {"title": "Best practices with Reactjs", "rating": 4.4, "category": "front-end"}}]

我希望结果看起来像这样:

test = {info[0]["author"]: [info[0]["book"], info[1]["book"]]}

我的尝试,除了这会删除整个第二个词典。

aList = {}
final = []

for i in info:
    for values in i.values():
        if values not in aList.values():
            aListi["author"] = values

print(aList)

不胜感激!

尝试使用groupby:

print({k: [book["book"] for book in g] for k, g in groupby(info, lambda x: x["author"])})

输出:

{'John Doe': [{'title': 'Getting started with Golang', 'category': 'programming', 'rating': 4.2}, {'title': 'Best practices with Reactjs', 'category': 'front-end', 'rating': 4.4}]}

这应该能满足您的需求;

info = [{"author": "John Doe", "book": {"title": "Getting started with Golang", "rating": 4.2, "category": "programming"}},
        {"author": "John Doe", "book": {"title": "Best practices with Reactjs", "rating": 4.4, "category": "front-end"}}]

authors = {}

for entry in info:
    authors.setdefault(entry['author'], []).append(entry['book'])

print(authors)

# Output
{'John Doe': [{'title': 'Getting started with Golang', 'rating': 4.2, 'category': 'programming'}, {'title': 'Best practices with Reactjs', 'rating': 4.4, 'category': 'front-end'}]}

这使用 setdefault 来初始化一个特定的键,在本例中是您的作者姓名,append 将项目添加到列表中。

你可以试试这个:

new_dict = pd.DataFrame(info).groupby(['author'])['book'].\
           apply(lambda x : x.tolist()).\
           to_dict()

new_dict

{'John Doe': [{'title': 'Getting started with Golang',
   'rating': 4.2,
   'category': 'programming'},
  {'title': 'Best practices with Reactjs',
   'rating': 4.4,
   'category': 'front-end'}]}

您可以使用 itertools.groupby 获取密钥,我建议使用 operator.itemgetter

from itertools import groupby
from operator import itemgetter

info = [{"author": "John Doe", "book": {"title": "Getting started with Golang", "rating": 4.2, "category": "programming"}},
        {"author": "John Doe", "book": {"title": "Best practices with Reactjs", "rating": 4.4, "category": "front-end"}}]

result = {k: [d['book'] for d in g] for k, g in groupby(info, itemgetter('author'))}

{'John Doe': [{'title': 'Getting startedwith Golang', 'rating': 4.2, 'category':'programming'},
              {'title': 'Best practices with Reactjs', 'rating': 4.4, 'category': 'front-end'}]}