如何访问和过滤具有多个值的字典?

How to acess and filter a dictonary with multiple values?

我正在使用 pymongo 访问 MongoDB 中的文档。 现在我想根据值标准是否为真来过滤返回的目录。

目前我的代码如下所示:

db = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = db.myclient["pricing"]
mycol = mydb["locations"]

mydoc = db["pricing"]["prices"].find_one({"location":"test"})

for i in mydoc.items():
    print(i)

此代码returns以下:

('_id', ObjectId('5f43c555dec06035ccfd2eac'))
('location', 'test')
('rent-40', {'titel': 'Base rent', 'price': 620, 'tooltip': 'test i think ', 'explenation': 'longer explanation', 'standard': False})
('rent-32', {'titel': 'Base rent', 'price': 600, 'tooltip': 'nana', 'explenation': 'very long explenation', 'standard': True})
('water', {'titel': 'Water cost', 'price': 10, 'tooltip': 'test', 'explenation': 'long explenation', 'standard': True})
  ('tv-38', {'titel': 'TV program', 'price': 5, 'tooltip': 'long', 'explenation': 'longer', 'standard': False})
('tv-70', {'titel': 'TV program', 'price': 5, 'tooltip': 'boring', 'explenation': 'very boring', 'standard': True})

最终结果应该是standard == True所在的词典列表。 我非常感谢您在这方面的帮助,因为我正在努力解决数据的嵌套方式。

也许不是最优雅的解决方案,但它对我有用。问题是前两个项目的格式不同,所以我把它们剪掉了,然后简单地退出了:

standard = []
c =list(mydoc.items())
#print(c[2:])
for i in c[2:]:
    if i[1]["standard"] == True:
        standard.append(i)
else:
    print("hi")
print(standard)

欢迎使用 Whosebug。您可以使用字典理解来过滤文档。在下面的示例中,isinstance(v, dict) 检查该项目是否为嵌入文档,并且 v.get('standard') is True 是您的过滤器。

filtered_doc = {k: v for k, v in mydoc.items() if isinstance(v, dict) and v.get('standard') is True}

工作示例:

import pprint

mydoc = {
    'location': 'test',
    'rent-40': {'titel': 'Base rent', 'price': 620, 'tooltip': 'test i think ', 'explenation': 'longer explanation', 'standard': False},
    'rent-32': {'titel': 'Base rent', 'price': 600, 'tooltip': 'nana', 'explenation': 'very long explenation', 'standard': True},
    'water': {'titel': 'Water cost', 'price': 10, 'tooltip': 'test', 'explenation': 'long explenation', 'standard': True},
    'tv-38': {'titel': 'TV program', 'price': 5, 'tooltip': 'long', 'explenation': 'longer', 'standard': False},
    'tv-70': {'titel': 'TV program', 'price': 5, 'tooltip': 'boring', 'explenation': 'very boring', 'standard': True}}

filtered_doc = {k: v for k, v in mydoc.items() if isinstance(v, dict) and v.get('standard') is True}
pprint.pprint(filtered_doc)

将给予:

{'rent-32': {'explenation': 'very long explenation',
             'price': 600,
             'standard': True,
             'titel': 'Base rent',
             'tooltip': 'nana'},
 'tv-70': {'explenation': 'very boring',
           'price': 5,
           'standard': True,
           'titel': 'TV program',
           'tooltip': 'boring'},
 'water': {'explenation': 'long explenation',
           'price': 10,
           'standard': True,
           'titel': 'Water cost',
           'tooltip': 'test'}}

您可以只使用列表理解(如果您不想在 MongoDB 本身中过滤它):

filtered_items = [x for x in mydoc.items() if x[1]['standard']]

这将创建一个项目列表,其中第二个元素(返回的字典)的值为 True(如果键 'standard' 不存在,只需使用 x[1].get('standard', False)相反)