修改多列的打印函数-Python

修改多列的打印函数-Python,python,string,printing,command-line-interface,Python,String,Printing,Command Line Interface,我正在开发一个命令行解释器,我有一个函数,可以以易于阅读的方式打印一长串字符串 功能是: 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:

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

功能是:

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               ...                  ....
修改自


我有一个解决方案,它也把终端宽度作为输入,只显示尽可能多的列。见:

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))

您可以将列数计算为
num\u columns=len(list)//100+2
。这将给你3列100,5列300(仍然是一个线性函数)。看起来你想要类似于[这个答案]。()。也许这对你的项目来说不是问题,但是你想如何处理列表中非常广泛的项目:
真的有毛发霉的奶酪烂土豆
@FMc,有趣的问题。在我的项目中不会出现这种情况,但我的警告是,CL上的打印内容整洁且易于阅读,因此,也许将
真的有毛、发霉、腐烂的土豆加奶酪
放在它自己的专栏末尾就可以了。或者在每个字符串之间加一个分隔符,以便您可以轻松地区分它们。
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))