简体   繁体   English

随机访问 python 中的字典值

[英]randomly accessing dictionary values in python

So I have code, where it should choose at random whether to show the user the entry itself or the associated definition.所以我有代码,它应该随机选择是向用户显示条目本身还是相关的定义。

from random import *


def show_flashcard():

    random_key = choice(list(glossary))
    print('Define: ', random_key)
    input('Press return to see the definition')
    print(glossary[random_key])

glossary = {'word1':'definition1',
            'word2':'definition2',
            'word3':'definition3'}

It only shows the random keys, but not the values.它只显示随机键,但不显示值。 How can i implement this in my code?我如何在我的代码中实现这一点?

With choice(list(glossary.values())) , you can get a random value from your dict.使用choice(list(glossary.values())) ,您可以从字典中获取随机值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM