简体   繁体   English

如何在Python中打印表格字典

[英]How to print a tabular dictionary in Python

Suppose I have a dictionary of the form: 假设我有以下形式的字典:

the_board = {(1,1) : ' ', (1,2) : ' ', (1,3) : ' ',
             (2,1) : ' ', (2,2) : ' ', (2,3) : ' ',
             (3,1) : ' ', (3,2) : ' ', (3,3) : ' ',}

I want to print each row line by line. 我想逐行打印每一行。 Currently I do something like this: 目前,我正在执行以下操作:

def display(board):
    var = list(board.values())  # Iterator to print out the table
    i = 0
    j = 0
    maxi = len(var)
    while i < maxi:
        while j < (i + 3):
            print(var[j], end="")
            if j < i+2:
                print('|', end='')
            j += 1
        print()
        if i < (maxi-1):
            print("-+-+-")
        i += 3

I am aware that this is most likely not the most "pythonic way" to achieve what I want. 我知道这很可能不是实现我想要的最“ Python方式”。 How would I do so in a more pythonic manner? 我将如何以更Python化的方式进行操作? (I am aware that I can use the keys to achieve this since I gave them coordinate keys, but I may need to print a tabular dictionary without ordered/subscripted keys, so I would appreciate a more general solution). (我知道我可以使用这些键来实现此目的,因为我给了它们坐标键,但是我可能需要在不使用有序/下标键的情况下打印表格字典,因此,我希望能找到更通用的解决方案)。

Found out about Python's range function, so now my code looks like this: 了解了Python的range函数,所以现在我的代码如下:

def display(board):
    var = list(board.values())  # Iterator to print out the table
    maxi = len(var)
    for i in range(0, maxi, 3):
        for j in range(i, (i+3)):
            print(var[j], end="")
            if j < i+2:
                print('|', end='')
        print()
        if i < (maxi-1):
            print("-+-+-")

Still not sure it's the best way to write it. 仍然不确定这是编写它的最佳方法。

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('-+-+-')

Hi If I understood you correctly this should be solution 嗨,如果我理解正确,应该可以解决

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)

Output Will Be 输出将是

在此处输入图片说明

You can set the number of columns: 您可以设置列数:

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)

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

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