Python random.choice方法没有重复?

时间:2015-03-18 17:23:56

标签: python python-3.x random

我试图改进这段代码,要求用户说出州政府的状态,但是我注意到有时它会重复一个州,然后问两次。

我尝试使用random.sample,但我收到错误" TypeError:Unhashable type:' list'。下面的代码可以使用random.sample注释掉(但重复):

capitals_dict = {
    'Alabama': 'Montgomery',
    'Alaska': 'Juneau',
    'Arizona': 'Phoenix',
    'Arkansas': 'Little Rock',
    'California': 'Sacramento',
    'Colorado': 'Denver',
    'Connecticut': 'Hartford',
    'Delaware': 'Dover',
    'Florida': 'Tallahassee',
    'Georgia': 'Atlanta',
    'Hawaii': 'Honolulu',
    'Idaho': 'Boise',
    'Illinois': 'Springfield',
    'Indiana': 'Indianapolis',
    'Iowa': 'Des Moines',
    'Kansas': 'Topeka',
    'Kentucky': 'Frankfort',
    'Louisiana': 'Baton Rouge',
    'Maine': 'Augusta',
    'Maryland': 'Annapolis',
    'Massachusetts': 'Boston',
    'Michigan': 'Lansing',
    'Minnesota': 'St. Paul',
    'Mississippi': 'Jackson',
    'Missouri': 'Jefferson City',
    'Montana': 'Helena',
    'Nebraska': 'Lincoln',
    'Nevada': 'Carson City',
    'New Hampshire': 'Concord',
    'New Jersey': 'Trenton',
    'New Mexico': 'Santa Fe',
    'New York': 'Albany',
    'North Carolina': 'Raleigh',
    'North Dakota': 'Bismarck',
    'Ohio': 'Columbus',
    'Oklahoma': 'Oklahoma City',
    'Oregon': 'Salem',
    'Pennsylvania': 'Harrisburg',
    'Rhode Island': 'Providence',
    'South Carolina': 'Columbia',
    'South Dakota': 'Pierre',
    'Tennessee': 'Nashville',
    'Texas': 'Austin',
    'Utah': 'Salt Lake City',
    'Vermont': 'Montpelier',
    'Virginia': 'Richmond',
    'Washington': 'Olympia',
    'West Virginia': 'Charleston',
    'Wisconsin': 'Madison',
    'Wyoming': 'Cheyenne',
}

import random

states = list(capitals_dict.keys())
for i in [1, 2, 3, 4, 5]:
    state = random.choice(states)
    #state = random.sample(states, 5) 
    capital = capitals_dict[state]
    capital_guess = input('What is the capital of ' + state + '?')

    if capital_guess == capital:
        print('Correct! Nice job!')
    else:
        print('Incorrect.  The Capital of ' + state + ' is ' + capital + '.')

print('All done.')

我也试过像这样使用词典名称capitals_dict:

random.sample(capitals_dict, 5)

但我得到了一个不同的错误,然后发现我不能使用这样的字典。

3 个答案:

答案 0 :(得分:4)

您可以先将字典传递给list()函数,然后从该列表中进行采样,从而创建字典中所有键的列表:

sample = random.sample(list(capitals_dict), 5)

您还可以传递dict.keys()字典视图:

sample = random.sample(capitals_dict.keys(), 5)

但内部random.sample()也会将其转换为序列(tuple()),因此使用list()实际上效率更高。

您实际遇到的异常告诉您:

>>> random.sample(capitals_dict, 5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python3.4/random.py", line 311, in sample
    raise TypeError("Population must be a sequence or set.  For dicts, use list(d).")
TypeError: Population must be a sequence or set.  For dicts, use list(d).
#                                                 ^^^^^^^^^^^^^^^^^^^^^^^

演示:

>>> import random
>>> capitals_dict = {
...     'Alabama': 'Montgomery',
...     'Alaska': 'Juneau',
...     'Arizona': 'Phoenix',
...     'Arkansas': 'Little Rock',
...     'California': 'Sacramento',
...     # ... elided ...
... }
>>> 
>>> random.sample(list(capitals_dict), 5)
['Maryland', 'Mississippi', 'Wisconsin', 'Texas', 'West Virginia']

将其合并到您的代码中:

导入随机

for state in random.sample(list(capitals_dict), 5):
    capital = capitals_dict[state]
    capital_guess = input('What is the capital of {}?'.format(state))

    if capital_guess == capital:
        print('Correct! Nice job!')
    else:
        print('Incorrect. The Capital of {} is {}.'.format(state, capital))

我还用str.format()调用替换了字符串连接,而是将值放入字符串模板中。

答案 1 :(得分:1)

尝试这样做。它只是对州名进行抽样:

import random

num_queries = 5

for state in random.sample(capitals_dict.keys(), num_queries):
    capital = capitals_dict[state]

    capital_guess = input('What is the capital of ' + state + '?')

    if capital_guess == capital:
        print('Correct! Nice job!')
    else:
        print('Incorrect.  The Capital of ' + state + ' is ' + capital + '.')

print('All done.')

虽然可以也使用:

for state in random.sample(list(capitals_dict), num_queries):

因为list(dictionary)将隐式返回字典键的列表,但我更喜欢明确地做出什么。

答案 2 :(得分:0)

如果有人在阅读此内容时想要一个体面的美国国家首都,我会更新代码以包括跟踪用户分数。它会以随机顺序询问所有50个州,它也会让你随时跳过并退出。

capitals_dict = {
    'Alabama': 'Montgomery',
    'Alaska': 'Juneau',
    'Arizona': 'Phoenix',
    'Arkansas': 'Little Rock',
    'California': 'Sacramento',
    'Colorado': 'Denver',
    'Connecticut': 'Hartford',
    'Delaware': 'Dover',
    'Florida': 'Tallahassee',
    'Georgia': 'Atlanta',
    'Hawaii': 'Honolulu',
    'Idaho': 'Boise',
    'Illinois': 'Springfield',
    'Indiana': 'Indianapolis',
    'Iowa': 'Des Moines',
    'Kansas': 'Topeka',
    'Kentucky': 'Frankfort',
    'Louisiana': 'Baton Rouge',
    'Maine': 'Augusta',
    'Maryland': 'Annapolis',
    'Massachusetts': 'Boston',
    'Michigan': 'Lansing',
    'Minnesota': 'St. Paul',
    'Mississippi': 'Jackson',
    'Missouri': 'Jefferson City',
    'Montana': 'Helena',
    'Nebraska': 'Lincoln',
    'Nevada': 'Carson City',
    'New Hampshire': 'Concord',
    'New Jersey': 'Trenton',
    'New Mexico': 'Santa Fe',
    'New York': 'Albany',
    'North Carolina': 'Raleigh',
    'North Dakota': 'Bismarck',
    'Ohio': 'Columbus',
    'Oklahoma': 'Oklahoma City',
    'Oregon': 'Salem',
    'Pennsylvania': 'Harrisburg',
    'Rhode Island': 'Providence',
    'South Carolina': 'Columbia',
    'South Dakota': 'Pierre',
    'Tennessee': 'Nashville',
    'Texas': 'Austin',
    'Utah': 'Salt Lake City',
    'Vermont': 'Montpelier',
    'Virginia': 'Richmond',
    'Washington': 'Olympia',
    'West Virginia': 'Charleston',
    'Wisconsin': 'Madison',
    'Wyoming': 'Cheyenne',
}

import random
counterQuestions = 0 # Represents the number of questions asked to the user
counterCorrect = 0
print('Enter the name of the State Capital with proper spelling.  Enter "skip" to skip or "quit" to quit')
for state in random.sample(list(capitals_dict), 50):
    capital = capitals_dict[state]
    capital_guess = input('What is the capital of {}? '.format(state))
    if capital_guess == 'skip':
        #print('The Capital of {} is {}.'.format(state, capital)) #study mode - use comment feature to turn this on/off.
        counterQuestions = counterQuestions + 1    
        continue
    elif capital_guess == 'quit':
        break
    elif capital_guess == capital:
        print('Correct! Nice job!')
        counterCorrect = counterCorrect + 1
        counterQuestions = counterQuestions + 1
    else:
        print('Incorrect.  The Capital of {} is {}.'.format(state, capital))
        counterQuestions = counterQuestions + 1

score = (counterCorrect / counterQuestions) * 100
counterIncorrect = counterQuestions - counterCorrect
print('All done. Your score is ' + str(score) + '% correct, or ' + str(counterCorrect) + ' out of ' + str(counterQuestions) + ' (' + str(counterIncorrect) + ' incorrect)')