从TinyDB数据库获取所有密钥

时间:2019-12-07 11:21:08

标签: python tinydb

我创建一个看起来像这样的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."}
    }
}

我想检索“单词”字段的每个值。那怎么可能?

1 个答案:

答案 0 :(得分:0)

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

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