简体   繁体   English

如何将字典格式化为 5x5 网格?

[英]How do you format a dictionary into a 5x5 grid?

I'm trying to print my dictionary into a 5x5 grid but dont know how to do that.我正在尝试将我的字典打印成 5x5 网格,但不知道该怎么做。

game_dict = {(0,0): 0, (0,1): 0, (0,2): 0, (0,3): 0, (0,4): 0, 
             (1,0): 0, (1,1): 0, (1,2): 0, (1,3): 0, (1,4): 0,
             (2,0): 0, (2,1): 0, (2,2): 0, (2,3): 0, (2,4): 0,
             (3,0): 0, (3,1): 0, (3,2): 0, (3,3): 0, (3,4): 0,
             (4,0): 0, (4,1): 0, (4,2): 0, (4,3): 0, (4,4): 0}

print(*game_dict.values())

This prints out 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0这打印出 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

I would like it to print我想打印

0 0 0 0 0
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0

current_position = (0, 0)当前位置 = (0, 0)

game_dict[str(current_position)] ="X" game_dict[str(current_position)] =“X”

X 0 0 0 0
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0

You can do what you want quite easily, by iterating through your coordinates:通过遍历坐标,您可以很容易地做您想做的事:

width = 5
height = 5
for y in range(height):
    row = [game_dict[(x, y)] for x in range(width)]
    print(*row)

However, if your coordinates are always integers, you can make this even simpler by just using a list of lists.但是,如果您的坐标始终是整数,则只需使用列表列表就可以使这更加简单。

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

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