如何创建字典列表?

How to create a list of dictionaries?

我需要创建字典列表:

box1 = {'x' : 10, 'y' : 10, 'direction' : 'right'}
box2 = {'x' : 20, 'y' : 10, 'direction' : 'right'}
box3 = {'x' : 30, 'y' : 10, 'direction' : 'right'}
box4 = {'x' : 40, 'y' : 10, 'direction' : 'right'}
box5 = {'x' : 50, 'y' : 10, 'direction' : 'right'}
box6 = {'x' : 60, 'y' : 10, 'direction' : 'right'}
box7 = {'x' : 70, 'y' : 10, 'direction' : 'right'}
box8 = {'x' : 80, 'y' : 10, 'direction' : 'right'}
box9 = {'x' : 90, 'y' : 10, 'direction' : 'right'}

我使用循环只成功创建了第一个字典框1

for i in range(9):
    new_n = 'box' + str(i+1)
    exec(new_n + " = {}")

我可以使用循环和 exec 方法来创建这样的列表吗? 如果没有,我该如何创建它?

P.S.: 我找不到列表理解的想法,它用字典填充列表并为这些字典分配不同的名称。这就是为什么这个问题不同于本网站上的任何其他问题。

boxes = {"box%d"%i:{'x' : x, 'y' : 10, 'direction' : 'right'} for i,x in enumerate(range(10,91,10),1)}

print boxes["box1"]

my_var = "box1"
print boxes[my_var]

我可能会这样做

一般来说你应该避免eval/exec