【发布时间】:2022-01-16 09:03:35
【问题描述】:
我今天给自己一个任务,只是想在 Python 中弄清楚下面的练习:
# given the dictionary below
dic = {
"jane": "doe",
"remy": "ma",
"haaland": "buuk",
"adam": "doe",
}
new_dict = {}
for x, y in dic.items():
if y not in new_dict.keys():
new_dict[y] = x
else:
new_dict[y] = [x]
print("results: ", new_dict)
# results: {'doe': ['adam'], 'ma': 'remy', 'buuk': 'haaland'}
我怎样才能达到以下结果?
results: {'doe': ['jane', 'adam'], 'ma': 'remy', 'buuk': 'haaland'}
【问题讨论】:
标签: python dictionary group-by