【问题标题】:Modify print function for multiple columns - Python修改多列的打印功能 - Python
【发布时间】:2013-11-08 16:01:27
【问题描述】:

我正在开发一个命令行解释器,我有一个函数可以以易于阅读的方式打印出一长串字符串。

函数是:

def pretty_print(CL_output):
    if len(CL_output)%2 == 0:
        #even
        print "\n".join("%-20s %s"%(CL_output[i],CL_output[i+len(CL_output)/2]) for i in range(len(CL_output)/2))    
    else:
        #odd
        d_odd = CL_output + ['']
        print "\n".join("%-20s %s"%(d_odd[i],d_odd[i+len(d_odd)/2]) for i in range(len(d_odd)/2))

所以,对于这样的列表:

myList = ['one','potato','two','potato','three','potato','four','potato'...]

函数 pretty_print 返回:

pretty_print(myList)

>>> one                  three
    potato               potato
    two                  four
    potato               potato

但是对于较大的列表,pretty_print 函数仍将列表打印成两列。有没有办法修改 pretty_print 以便根据列表的大小在 3 或 4 列上打印出一个列表?所以 len(myList) ~ 100, pretty_print 将打印 3 行,而对于 len(myList) ~ 300,pretty_print 将打印 4 列。

如果我有:

  myList_long = ['one','potato','two','potato','three','potato','four','potato'...
           'one hundred`, potato ...... `three hundred`,potato]

想要的输出是:

pretty_print(myList_long)

>>> one                  three                one hundred          three hundred
    potato               potato               potato               potato
    two                  four                 ...                  ...
    potato               potato               ...                  ....

【问题讨论】:

  • 您可以将列数计算为num_columns = len(list) // 100 + 2。这将为 100 提供 3 列,为 300 提供 5 列(同时仍然是线性函数)。
  • 看起来你想要类似 [this answer].(stackoverflow.com/a/1524333/443348).
  • 也许这对您的项目来说不是问题,但是您希望如何处理列表中非常的项目:really-hairy-moldy-rotten-potato-with-cheese.
  • @FMc,有趣的问题。这不会发生在我的项目中,但我需要注意的是 CL 上的打印输出整洁且易于阅读,因此也许将 really-hairy-moldy-rotten-potato-with-cheese 放在它自己的最后一列中可能会起作用。或者在每个字符串之间放置一个分隔符,以便您轻松区分它们。

标签: python string printing command-line-interface


【解决方案1】:

修改自this answer

def pretty_print(CL_output):
    columns = len(CL_output)//200+2
    lines = ("".join(s.ljust(20) for s in CL_output[i:i+columns-1])+CL_output[i:i+columns][-1] for i in range(0, len(CL_output), columns))
    return "\n".join(lines)

【讨论】:

    【解决方案2】:

    我有一个解决方案,它也将终端宽度作为输入,并仅显示可以容纳的列数。见:https://gist.github.com/critiqjo/2ca84db26daaeb1715e1

    col_print.py

    def col_print(lines, term_width=80, indent=0, pad=2):
      n_lines = len(lines)
      if n_lines == 0:
        return
    
      col_width = max(len(line) for line in lines)
      n_cols = int((term_width + pad - indent)/(col_width + pad))
      n_cols = min(n_lines, max(1, n_cols))
    
      col_len = int(n_lines/n_cols) + (0 if n_lines % n_cols == 0 else 1)
      if (n_cols - 1) * col_len >= n_lines:
        n_cols -= 1
    
      cols = [lines[i*col_len : i*col_len + col_len] for i in range(n_cols)]
    
      rows = list(zip(*cols))
      rows_missed = zip(*[col[len(rows):] for col in cols[:-1]])
      rows.extend(rows_missed)
    
      for row in rows:
        print(" "*indent + (" "*pad).join(line.ljust(col_width) for line in row))
    

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2013-07-05
      • 2011-11-19
      相关资源
      最近更新 更多