从Python字典中选择随机键中的随机值

时间:2012-08-18 22:34:21

标签: python function dictionary python-2.7

正如我之前所说,我正在尝试进行多项选择测验。测验会随机抓取3个键作为“答案”。然后测验将获取3个选定键的值并将其用作“问题”。我正在尝试使用random.sample作为参数从随机选择的键值中选择一个值。我的代码如下:

import random

word_drills = {'class': 'Tell Python to make a new kind of thing.',
               'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.',
               'instance': 'What you get when you tell Python to create a class.',
               'def': 'How you define a function inside a class.',
               'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.',
               'inheritance': 'The concept that one class can inherit traits from another class, much like you and your parents.',
               'composition': 'The concept that a class can be composed of other classes as parts, much like how a car has wheels.',
               'attribute': 'A property classes have that are from composition and are usually variables.',
               'is-a': 'A phrase to say that something inherits from another, as in a Salmon *** Fish',
               'has-a': 'A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.'}


def nodupchoice():
    key1, key2, key3 = random.sample(word_drills, 3)
    print "%s, %s, %s" % (key1, key2, key3)
    #print word_drills.random.sample[key1, key2, key3]
    #print word_drills[key1]
    #print word_drills[random.sample(key1, key2, key3)]


nodupchoice()

你会注意到我已经评论了我认为实际上会给我的结果。不幸的是,每个人都没有。任何指导协助将不胜感激。再次感谢。

更新

所以我已经提供了所提供的新信息。再次感谢你。这就是我想出的:

import random

word_drills = {'class': 'Tell Python to make a new kind of thing.',
               'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.',
               'instance': 'What you get when you tell Python to create a class.',
               'def': 'How you define a function inside a class.',
               'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.',
               'inheritance': 'The concept that one class can inherit traits from another class, much like you and your parents.',
               'composition': 'The concept that a class can be composed of other classes as parts, much like how a car has wheels.',
               'attribute': 'A property classes have that are from composition and are usually variables.',
               'is-a': 'A phrase to say that something inherits from another, as in a Salmon *** Fish',
               'has-a': 'A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.'}


def nodupchoice():
    # For loop that creates a list named keys. It grabs 3 random keys from the dictionary word_drills
    keys = [x for x in random.sample(word_drills, 3)]
    # User is presented with a question. A value from the previous randomly selected keys is selected as the 'question'
    print "Question: ", word_drills[random.choice(keys)]
    # Set the variables key1, key2, & key3 to the 3 keys in the list 'keys'
    key1, key2, key3 = keys[0], keys[1], keys[2]
    # User is presented with 3 choices.
    print "\n\n(a)%s   (b)%s   (c)%s" % (key1, key2, key3)
    selection = raw_input("> ")
    print selection

nodupchoice()

我现在试图破译的问题是:如何根据字典word_drills中的内容检查用户选择。我打算使用if / else。如果它是正确的,它会通知你,否则你是不正确的。但是不知道如何处理这个问题。只是想再次感谢大家的帮助,我们非常感激。

2 个答案:

答案 0 :(得分:2)

我想你想做的是:

print word_drills[random.choice([key1, key2, key3])]

这将打印属于其中一个随机采样键的值。

答案 1 :(得分:1)

您可能想尝试使用random.choice()来选择密钥:

keys = random.sample(word_drills, 3)
print keys
print word_drills[random.choice(keys)]

输出:

['has-a', 'attribute', 'class']
A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.