从 TinyDB 数据库中获取所有的密钥
Get all the keys from TinyDB database
我创建了一个 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" 的每个值。这怎么可能?
您可以检索所有行,然后解析以获取 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
我创建了一个 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" 的每个值。这怎么可能?
您可以检索所有行,然后解析以获取 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