代码之家  ›  专栏  ›  技术社区  ›  Tony Veijalainen

如何将pretty print模块扩展到表?

  •  -1
  • Tony Veijalainen  · 技术社区  · 16 年前

        >>> a=range(10)
        >>> a.insert(5,[range(i) for i in range(10)])
        >>> a
        [0, 1, 2, 3, 4, [[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]], 5, 6, 7, 8, 9]
        >>> import pretty
        >>> pretty.ppr(a,indent=6)
    
        [0, 1, 2, 3, 4, 
              [
                [], 
                [0], 
                [0, 1], 
                [0, 1, 2], 
                [0, 1, 2, 3], 
                [0, 1, 2, 3, 4], 
                [0, 1, 2, 3, 4, 5], 
                [0, 1, 2, 3, 4, 5, 6], 
                [0, 1, 2, 3, 4, 5, 6, 7], 
                [0, 1, 2, 3, 4, 5, 6, 7, 8]], 5, 6, 7, 8, 9]
    

    代码如下:

    """ pretty.py prettyprint module version alpha 0.2
        mypr: pretty string function
        ppr:  print of the pretty string
        ONLY list and tuple prettying implemented!
    """
    def mypr(w, i = 0, indent = 2, nl = '\n') :
        """ w = datastructure, i = indent level, indent = step size for indention """
        startend = {list : '[]', tuple : '()'}
        if type(w) in (list, tuple) :
            start, end = startend[type(w)]
            pr = [mypr(j, i + indent, indent, nl) for j in w]
            return nl + ' ' * i + start + ', '.join(pr) + end
        else :  return repr(w)
    
    def ppr(w, i = 0, indent = 2, nl = '\n') :
        """ see mypr, this is only print of mypr with same parameters """
        print mypr(w, i, indent, nl)
    

    ## let's do it "manually"
    width = len(str(10+10))
    widthformat = '%'+str(width)+'i'
    for i in range(10):
        for j in range(10):
            print widthformat % (i+j),
        print
    

    你有没有更好的替代方案,让这段代码对于漂亮的打印模块来说足够通用?

    在发布问题后,我发现这类常规案例有以下模块: prettytable A simple Python library for easily displaying tabular data in a visually appealing ASCII table format

    4 回复  |  直到 16 年前
        1
  •  7
  •   John Kugelman Michael Hodel    16 年前

    如果你在寻找矩阵的好格式, numpy 的输出看起来非常棒:

    from numpy import *
    print array([[i + j for i in range(10)] for j in range(10)])
    

    输出:

    [[ 0  1  2  3  4  5  6  7  8  9]
     [ 1  2  3  4  5  6  7  8  9 10]
     [ 2  3  4  5  6  7  8  9 10 11]
     [ 3  4  5  6  7  8  9 10 11 12]
     [ 4  5  6  7  8  9 10 11 12 13]
     [ 5  6  7  8  9 10 11 12 13 14]
     [ 6  7  8  9 10 11 12 13 14 15]
     [ 7  8  9 10 11 12 13 14 15 16]
     [ 8  9 10 11 12 13 14 15 16 17]
     [ 9 10 11 12 13 14 15 16 17 18]]
    
        2
  •  3
  •   Jochen Ritzel    16 年前

    你可以写:

    '\n'.join(  # join the lines with '\n'
           ' '.join(  # join one line with ' '
                  "%2d" % (i + j) # format each item
            for i in range(10))
        for j in range(10))
    
        3
  •  1
  •   unutbu    16 年前

    使用 George Sakkis' table indention recipe :

    print(indent(((i + j for i in range(10)) for j in range(10)),
                 delim=' ', justify='right'))
    

    产量:

    0  1  2  3  4  5  6  7  8  9
    1  2  3  4  5  6  7  8  9 10
    2  3  4  5  6  7  8  9 10 11
    3  4  5  6  7  8  9 10 11 12
    4  5  6  7  8  9 10 11 12 13
    5  6  7  8  9 10 11 12 13 14
    6  7  8  9 10 11 12 13 14 15
    7  8  9 10 11 12 13 14 15 16
    8  9 10 11 12 13 14 15 16 17
    9 10 11 12 13 14 15 16 17 18
    

    另外,为了使上述工作,我做了一个小的改变配方。我变了 wrapfunc(item) wrapfunc(str(item)) :

    def rowWrapper(row):
        newRows = [wrapfunc(str(item)).split('\n') for item in row]