Pythonic 方式反转嵌套字典
我有一个包含人员和项目评级的嵌套字典,以人员为键。人们可能会也可能不会共享物品。 示例:
{
'Bob' : {'item1':3, 'item2':8, 'item3':6},
'Jim' : {'item1':6, 'item4':7},
'Amy' : {'item1':6,'item2':5,'item3':9,'item4':2}
}
我正在寻找最简单的方法来翻转这些关系,并有一个以项目为键的新嵌套字典。 示例:
{'item1' : {'Bob':3, 'Jim':6, 'Amy':6},
'item2' : {'Bob':8, 'Amy':5},
'item3' : {'Bob':6, 'Amy':9},
'item4' : {'Jim':7, 'Amy':2}
}
执行此操作的最佳方法是什么?有了悟性就可以了吗?
I have a nested dictionary of people and item ratings, with people as the key. people may or may not share items.
Example:
{
'Bob' : {'item1':3, 'item2':8, 'item3':6},
'Jim' : {'item1':6, 'item4':7},
'Amy' : {'item1':6,'item2':5,'item3':9,'item4':2}
}
I'm looking for the simplest way to flip these relations, and have a new nested dictionary with items as the key.
Example:
{'item1' : {'Bob':3, 'Jim':6, 'Amy':6},
'item2' : {'Bob':8, 'Amy':5},
'item3' : {'Bob':6, 'Amy':9},
'item4' : {'Jim':7, 'Amy':2}
}
What is the best way to do this? Is it possible with a comprehension?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
collections.defaultdict 使这变得非常简单:
输出:
collections.defaultdict makes this pretty simple:
Output:
我完全同意 Ryan Ginstrom 的答案是执行此操作的首选方式(出于所有实际目的)。
但由于这个问题也明确地问:
理解是否可能?
我想我应该用一个简单的例子来说明如何使用列表理解来做到这一点(这可能是一个很好的例子,展示了嵌套列表理解如何快速降低可读性)。
I totally agree that Ryan Ginstrom's answer is the preferred way of doing this (for all practical purposes).
But since the question also explicitely asks:
Is it possible with a comprehension?
I thought I'd chime in with a quick example as for how to do this with a list comprehension (it could be a good example for showing how nested list comphrehensions can quickly decrease readability).
这很容易做到(正如其他人所展示的那样),但根据您的需求,您还应该考虑,对于要按任何标准提取的包含多条信息的数据,数据库可能是最好的工具。内置的 sqlite3 模块提供了一个低开销的数据库,根据您正在做的事情,它可能比嵌套字典更好地为您服务。
This is easy enough to do (as others have shown), but depending on your needs you should also consider that for data with several pieces of information where you want to extract by any criterion, a database might be the best tool. The built-in
sqlite3
module provides a low-overhead database that may, depending on what you are doing, serve you better than a nested dict.如果您只想访问反向嵌套字典,
如果字典太大而无法反转,请节省内存。
If you want just access reverse nested dictionaries,
Save memory if the dictionary is too large to reverse.
熊猫可以提供另一种选择。假设
data
是输入字典。Pandas can provide another option. Assume
data
is the input dictionary.