How to print a list more nicely?

前端 未结 22 1265
北荒
北荒 2020-12-01 09:34

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 b

22条回答
  •  春和景丽
    2020-12-01 10:17

    from itertools import izip_longest, islice
    L = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf', 
        'pdcurses-devel',     'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel', 
        'qgis1.1', 'php_mapscript']
    
    def columnize(sequence, columns=2):
        size, remainder = divmod(len(sequence), columns)
        if remainder: 
            size += 1
        slices = [islice(sequence, pos, pos + size) 
                  for pos in xrange(0, len(sequence), size)]
        return izip_longest(fillvalue='', *slices)
    
    for values in columnize(L):
        print ' '.join(value.ljust(20) for value in values)
    

提交回复
热议问题