3 回答
TA贡献1815条经验 获得超6个赞
def chunks(l,n):
""" Split list into chunks of size n """
for i in range(0, len(l), n):
yield l[i:i+n]
def display(board):
for values in chunks(list(the_board.values()), 3):
print('|'.join(values)) # use str.join to concat strings with separators
print('-+-+-')
TA贡献1850条经验 获得超11个赞
嗨,如果我理解正确的话,这应该是解决方案
board = {(1,1) : ' a ', (1,2) : ' b ', (1,3) : ' c ',
(2,1) : 'd ', (2,2) : 'e ', (2,3) : ' f ',
(3,1) : 'g ', (3,2) : ' h', (3,3) : ' i',}
print ( "Cordiantes --- Values")
for key , value in board.items():
print(key , " " , value)
输出将是
TA贡献1848条经验 获得超10个赞
您可以设置列数:
the_board = {
(1, 1): ' ', (1, 2): ' ', (1, 3): ' ',
(2, 1): ' ', (2, 2): ' ', (2, 3): ' ',
(3, 1): ' ', (3, 2): ' ', (3, 3): ' '
}
def display(board, ncols):
items = list(board.values())
separate_line = '\n' + '+'.join('-' * ncols) + '\n'
item_lines = []
i = 0
while i + ncols <= len(items):
item_line = '|'.join(items[i:i + ncols])
item_lines.append(item_line)
i += ncols
output = separate_line.join(item_lines)
print(output)
display(the_board, ncols=3)
添加回答
举报