简体   繁体   English

从名称列表中创建大量词典

[英]Creating lots of dictionaries from a list of names

I am making a times table/ division game where I have created dictionaries for each times table and division (up to 12) made up of the question as the key and answer as the value. 我正在制作一个时间表/除法游戏,其中我为每个时间表和除法(最多12个)创建了词典,该词典由问题作为键,而答案作为值。 That is 24 dictionaries! 那是24本字典! There is surely a better way of doing this than what I have done, perhaps creating a for loop. 当然,有比我做的更好的方法,也许是创建一个for循环。

I have managed to make each times table/ division dictionary using the following loop code (24 times; 12 for the multiple tables and 12 for the division equivalents) I have included an example of a multiple dictionary as well as a division one. 我设法使用以下循环代码制作了每个表/除法字典(24次; 12个用于多个表,12个用于除法等效项)我提供了一个多重字典和除法示例。

eleven_division = {}
for i in range (0,13):
    division_factor = 11 
    answer = i * division_factor
    eleven_division ['{} / {} ='.format(answer,division_factor)] = i

six_times = {}
for i in range (0,13):
    multiple = 6
    answer = i * multiple
    six_times ['{} x {} ='.format(i,multiple)] = answer

I would like to create all the dictionaries using nested for loops, but have each dictionary named in the following way: two_times, three_times, four_times. 我想使用嵌套的for循环创建所有字典,但是用以下方式命名每个字典:two_times,three_times,four_times。 However, I can figure out a way to do this. 但是,我可以找出一种方法。

I know I can do this to print out all the division tables. 我知道我可以执行此操作以打印出所有除法表。

for x in range (1,13):
    division_factor = x
    for i in range (0,13):
        answer = i * division_factor
        print ('%s / %s = %s' %(answer, x, i))

I now need to create a new dictionary every time is goes through the loop so I thought the following might do it, but no! 现在,每次循环时我都需要创建一个新字典,所以我认为以下方法可以做到,但是没有!

    dicts =['one division', 'two division','three division','four division', 'five division', 'six division', 'seven division', 'eight division', 'nine division', 'ten division', 'eleven division', 'twelve division']
for f in dicts:
    f = {} 
    for x in range (1,13):
        division_factor = x
        for i in range (0,13):
            answer = i * division_factor
            f ['{} x {} ='.format(answer,x)] = i

print (one division) #THIS IS THE BIT I WANT WORKING!

I hope I have made my issue clear enough for you. 我希望我已经为您澄清了我的问题。

TIA! TIA!

If I understand your problem, you've got 12 separate division dicts, and you don't know how to do anything with them without some horrible code like this: 如果我理解您的问题,那么您有12个单独的除法指令,并且您不知道如何在没有可怕的代码的情况下对它们执行任何操作:

if i==1: table = one_division
elif i==2: table = two_division
# … 10 more lines of code
do_something_with(table)

The answer is that you don't want those dicts in 12 separate variables, you want them in a dict, so you can just look them up like this: 答案是,您不希望将这些字典放在12个单独的变量中,而是希望将它们放在字典中,因此可以像这样查找它们:

do_something_with(division_tables[i])

One line instead of 13, and easier to understand, too. 一行而不是13行,也更容易理解。

You can generate this dict in a loop: 您可以循环生成此字典:

division_tables = {}
for division_factor in range(1, 13):
    table = {}
    for i in range (0,13):
        answer = i * division_factor
        table['{} / {} ='.format(answer,division_factor)] = i
    division_tables[division_factor] = table

And now you can write a game like this: 现在您可以编写如下游戏:

factor = random.choide(division_tables)
table = division_tables[factor]
question = random.choice(table)
answer = table[question]
guess = int(input(question))
if guess == answer:
    print('You are correct, sir!')
else:
    print('Hahahaha, a 20-line Python script has outsmarted you’')

If you want these dicts in a useful order, so you can choose an easy question by picking, say, one of the first three tables, then you probably want a list rather than a dict: 如果您希望这些字典按有用的顺序排列,则可以通过选择前三个表之一来选择一个简单的问题,那么您可能需要一个列表而不是字典:

division_tables = []
for division_factor in range(1, 13):
    table = {}
    for i in range (0,13):
        answer = i * division_factor
        table['{} / {} ='.format(answer,division_factor)] = i
    division_tables.append(table)

And now you can just do: 现在,您可以执行以下操作:

print('Easy question first:')
table = random.choice(division_tables[:3])
# … the rest of the code is the same as before

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

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