如何更好地打印列表? [英] How to print a list more nicely?
本文介绍了如何更好地打印列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这类似于如何在 Python 中很好地打印列表"",但我想更漂亮地打印列表——没有括号、撇号和逗号,甚至在列中更好.
This is similar to How to print a list in Python "nicely", but I would like to print the list even more nicely -- without the brackets and apostrophes and commas, and even better in columns.
foolist = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
'qgis1.1', 'php_mapscript']
evenNicerPrint(foolist)
想要的结果:
exiv2-devel msvcrt
mingw-libs gdal-grass
tcltk-demos iconv
fcgi qgis-devel
netcdf qgis1.1
pdcurses-devel php_mapscript
谢谢!
推荐答案
虽然不是为此设计的,但 Python 3 中的标准库模块 cmd
具有打印多列字符串列表的实用程序
Although not designed for it, the standard-library module in Python 3 cmd
has a utility for printing a list of strings in multiple columns
import cmd
cli = cmd.Cmd()
cli.columnize(foolist, displaywidth=80)
您甚至可以选择指定输出位置,使用 cmd.Cmd(stdout=my_stream)
You even then have the option of specifying the output location, with cmd.Cmd(stdout=my_stream)
这篇关于如何更好地打印列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文