【问题标题】:How to acess and filter a dictonary with multiple values?如何访问和过滤具有多个值的字典?
【发布时间】:2020-12-13 06:42:11
【问题描述】:

我正在使用 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)

此代码返回以下内容:

('_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)

【问题讨论】:

    标签: python dictionary filter pymongo


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      欢迎来到stackoverflow。您可以使用 dict 理解来过滤文档。在下面的示例中,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'}}
      

      【讨论】:

        猜你喜欢
        • 2021-06-14
        • 1970-01-01
        • 1970-01-01
        • 2019-04-23
        • 1970-01-01
        • 1970-01-01
        • 2022-06-10
        • 1970-01-01
        • 2017-12-03
        相关资源
        最近更新 更多