繁体   English   中英

随机访问 python 中的字典值

[英]randomly accessing dictionary values in python

所以我有代码,它应该随机选择是向用户显示条目本身还是相关的定义。

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'}

它只显示随机键,但不显示值。 我如何在我的代码中实现这一点?

使用choice(list(glossary.values())) ,您可以从字典中获取随机值。

暂无
暂无

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

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