所以我正在尝试通过Python设置多项选择测验。我对Python很陌生,所以如果有更简单的方法可以预先表示道歉。但是,在尝试更新的技术之前,我正试图真正理解一些基础知识。
我有一本字典。在这本字典中,我想要抓住3个随机密钥。我还想确保这三个键不相等(换句话说,彼此随机)。这是我到目前为止编写的代码:
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.'}
key1 = ' '
key2 = ' '
key3 = ' '
def nodupchoice():
while key1 == key2 == key3:
key1 = random.choice(word_drills.keys())
key2 = random.choice(word_drills.keys())
key3 = random.choice(word_drills.keys())
nodupchoice()
print "Key #1: %s, Key #2: %s, Key #3: %s" % (key1, key2, key3)
我很确定问题在于我的while循环。我想创建一个将继续运行的功能,直到所有三个键彼此不同。最后,它会打印结果。有任何想法吗?提前谢谢。
答案 0 :(得分:12)
您可以使用random.sample
:
>>> random.sample(word_drills, 3)
['has-a', 'attribute', 'instance']
并且您不需要.keys()
,字典上的迭代就在键上。
请注意,random.sample
将从您提供的列表中返回三个唯一值(即它永远不会返回'has-a'
两次):
>>> all(len(set(random.sample(word_drills, 3))) == 3 for i in range(10**5))
True
答案 1 :(得分:3)
使用random.sample
>>> import random
>>> random.sample([1,2,3,4,5,6], 3)
[4, 5, 2]
答案 2 :(得分:0)
最简单的事情可能是shuffle
键:
keysShuffled = list(word_drills)
random.shuffle(keysShuffled)
然后拿第3个
threeUnique = keysShuffled[:3]
答案 3 :(得分:0)
也许您想尝试Quiz Me 2.5?它是一个多选择的测验系统,在Python 3.x中有一个GUI。
答案 4 :(得分:0)
正如其他人所解释的那样,random.sample
是做你想做的最好的方式。
解释原始代码无效的原因:
第一个问题,你的while
循环存在逻辑错误 - 只要两个项目不同(例如'a' == 'a' == 'b'
为True,循环结束),它就会结束,以解决此问题,有几种方法:
while not (key1 != key2 != key3):
或者,set
只能包含唯一商品,因此当set([key1, key2, key3])
的长度为3时,所有三个值都不同:
while len(set([key1, key2, key3]) != 3:
第二个问题,以及代码拒绝运行的原因:
key1
key2
key3
被定义为全局变量(key1 = ' ' are at the top-level of your
。py`文件,不在函数中)
您可以在函数中查找全局变量而不会出现问题(例如您完成while key1 == key2 ...
的位置)。发生错误是因为您还尝试在循环中将分配给key1
- 这会混淆Python的解析器,因为您在while key1 == ...
部分查找全局变量,但尝试在下一行创建一个新的函数局部变量,因此它有助于抛出错误。
解决这个问题的垃圾方法是这样的:
key1 = ' '
key2 = ' '
key3 = ' '
def nodupchoice():
global key1
global key2
global key3
while not (key1 != key2 != key3):
key1 = random.choice(word_drills.keys())
key2 = random.choice(word_drills.keys())
key3 = random.choice(word_drills.keys())
然后它会按照您的意图工作,但不要这样做 - 全局变量have their uses,但这不是这种情况..
您可以轻松地从函数中返回多个值,而不是使用全局变量:
def nodupchoice():
key1 = ' ' # local variables, which are inaccessible outside this function
key2 = ' '
key3 = ' '
while not (key1 != key2 != key3):
key1 = random.choice(word_drills.keys())
key2 = random.choice(word_drills.keys())
key3 = random.choice(word_drills.keys())
return key1, key2, key3
key1, key2, key3 = nodupchoice()
print "Key #1: %s, Key #2: %s, Key #3: %s" % (key1, key2, key3)
哦,甚至更好,你可以传递word_drills
作为参数:
def nodupchoice(things):
key1 = ' ' # local variables, which are inaccessible outside this function
key2 = ' '
key3 = ' '
while not (key1 != key2 != key3):
key1 = random.choice(things)
key2 = random.choice(things)
key3 = random.choice(things)
return key1, key2, key3
key1, key2, key3 = nodupchoice(word_drills)
...你写的函数与random.sample(word_drills, 3)
几乎相同!
答案 5 :(得分:0)
我有这个samme问题并写了https://github.com/robtandy/randomdict来解决它。也许它也会帮助你。
它提供了一个python dict对象,但使用了其他方法,random_key,random_value和random_item。全部具有O(1)运行时复杂性。