【问题标题】:Get all the keys from TinyDB database从 TinyDB 数据库中获取所有密钥
【发布时间】:2019-12-07 11:21:08
【问题描述】:

我创建了一个如下所示的 tinydb 数据库:

{"_default": 
    {"1": {"word": "cat", "definition": "Its an animal."}, 
     "2": {"word": "cat", "definition": "Its an animal."}, 
     "3": {"word": "man", "definition": "has a hand"}, 
     "4": {"word": "girl", "definition": "Its a kid"}, 
     "5": {"word": "superman", "definition": "A hero."}
    }
}

我想检索字段“word”的每个值。这怎么可能?

【问题讨论】:

    标签: python tinydb


    【解决方案1】:

    您可以检索所有行,然后解析以获取 set 中不同的 word 字段,例如

    all_rows = db.all()
    
    all_words = {row['word'] for row in all_rows}     # set comprehension
    all_words = set(map(lambda x: x['home'], values)) # map operation
    

    【讨论】:

      猜你喜欢
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      相关资源
      最近更新 更多