Reputation: 18104
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)
Desired result:
exiv2-devel msvcrt
mingw-libs gdal-grass
tcltk-demos iconv
fcgi qgis-devel
netcdf qgis1.1
pdcurses-devel php_mapscript
thanks!
Upvotes: 36
Views: 73652
Reputation: 41
This will also do it cleanly and natively in Python3:
foolist = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
'pdcurses-devel', 'msvcrt', 'gdal-grass','iconv',
'qgis-devel', 'qgis1.1', 'php_mapscript']
for i in range(0, len(foolist), 2):
# Print each pair of items in the list (left and right columns)
package1 = foolist[i]
# Check if the next element exists
package2 = foolist[i+1] if i+1 < len(foolist) else ''
print(f"{package1:<25} {package2:<25}")
Output:
tcltk-demos fcgi
netcdf pdcurses-devel
msvcrt gdal-grass
iconv qgis-devel
qgis1.1 php_mapscript
Upvotes: 1
Reputation: 865
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=40)
Output:
exiv2-devel msvcrt
mingw-libs gdal-grass
tcltk-demos iconv
fcgi qgis-devel
netcdf qgis1.1
pdcurses-devel php_mapscript
You even then have the option of specifying the output location, with cmd.Cmd(stdout=my_stream)
Upvotes: 30
Reputation: 876
Here is the short and simple method:
def printList1(list, col, STR_FMT='{}', gap=1):
list = [STR_FMT.format(x).lstrip() for x in list]
FMT2 = '%%%ds%%s' % (max(len(x) for x in list)+gap)
print(''.join([FMT2 % (v, "" if (i+1) % col else "\n") for i, v in enumerate(list)]))
Then here is the better method which can turn off the constant width for the maximum string width across the entire list instead optimizing it across each column individually, restrict the number of columns to fit the max characters per line or find the optimal number of columns to fit the max characters per line, then force left or right justify, and can still can take the optional format string, pre-string, sep-string, and end-string. Also this allows for ordering of the 1D list to start column first or row first.
def getStrColList(aList, col=None, uniform=False, ljust=True, colFirst=False, pre=' ', sep=' ', end='', fmt="{}", maxL_len=120, minC_len=0, isPrint=True):
aList = [fmt.format(x).strip() for x in aList] # Compile the string outputs
N, gap, pad = len(aList), len(sep), len(pre)+len(end) # Get number of elements, gap str length, pad str length
c = max(1, min(col, N)) if col else N # Initialize column length or start at size of array.
# Iterate each column size to find the optimal number of columns suitable for number of maxL_len
while True:
# Generate List of List column indices and final string outputs
if colFirst: # Column to column ordering
LLs = list(map(lambda x: len(x), [list(range(N))[i::c] for i in range(c)]))
iLoL = list(map(lambda x: [x[1] + r for r in range(x[0])], [(LLs[i], sum(LLs[:i])) for i in range(c)]))
LoL = [[aList[iLoL[j][i]] for i in range(len(iLoL[j]))] for j in range(c)]
else: # Row to column ordering
LoL = [aList[j::c] for j in range(c)]
# Find max column string lengths and line string length
Cmaxs = [max([max(len(s), minC_len) for s in L]) if isinstance(L, list) else max(len(L), minC_len) for L in LoL]
LineLen = (c * max(Cmaxs) if uniform else sum([gap + L for L in Cmaxs]))+pad
if LineLen <= maxL_len or col is not None or c == 1:
break
c -= 1
Cmax = max(minC_len, max(Cmaxs)) if minC_len else (max(Cmaxs) if uniform else None)
# Create final output string
ljust, Nrows = '-' if ljust else '', max([len(cL) if isinstance(cL, list) else 1 for cL in LoL])
colFMT = [f"%%%s%ds%s" % (ljust, (Cmax if Cmax else m), end if i + 1 == c else '') for i, m in enumerate(Cmaxs)]
outStr = [[cF % v for v in L] for L, cF in zip(LoL, colFMT)]
outStr = '\n'.join([pre + sep.join([outStr[cc][r] for cc in range(c) if r < len(LoL[cc])]) for r in range(Nrows)])\
+ ('' if (N % c) == 0 else end)
if isPrint:
print(outStr)
return outStr
Testing w/ outputs:
>>> foolist = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
'qgis1.1', 'php_mapscript']
>>> for i in range(1, 10):
max_line_arg = maxL_len=15*i
str_out = getStrColList(foolist, colFirst=False, ljust=False, pre=' ', sep=' ', maxL_len=max_line_arg, isPrint=False)
for i, L in enumerate(str_out.splitlines()):
print(f'{len(L):3d} {"<=" if len(L) <= max_line_arg else " >"} {max_line_arg:<3d} | {L}')
print()
15 <= 15 | exiv2-devel
15 <= 15 | mingw-libs
15 <= 15 | tcltk-demos
15 <= 15 | fcgi
15 <= 15 | netcdf
15 <= 15 | pdcurses-devel
15 <= 15 | msvcrt
15 <= 15 | gdal-grass
15 <= 15 | iconv
15 <= 15 | qgis-devel
15 <= 15 | qgis1.1
15 <= 15 | php_mapscript
28 <= 30 | exiv2-devel mingw-libs
28 <= 30 | tcltk-demos fcgi
28 <= 30 | netcdf pdcurses-devel
28 <= 30 | msvcrt gdal-grass
28 <= 30 | iconv qgis-devel
28 <= 30 | qgis1.1 php_mapscript
40 <= 45 | exiv2-devel mingw-libs tcltk-demos
40 <= 45 | fcgi netcdf pdcurses-devel
40 <= 45 | msvcrt gdal-grass iconv
40 <= 45 | qgis-devel qgis1.1 php_mapscript
56 <= 60 | exiv2-devel mingw-libs tcltk-demos fcgi
56 <= 60 | netcdf pdcurses-devel msvcrt gdal-grass
56 <= 60 | iconv qgis-devel qgis1.1 php_mapscript
62 <= 75 | exiv2-devel mingw-libs tcltk-demos fcgi netcdf
62 <= 75 | pdcurses-devel msvcrt gdal-grass iconv qgis-devel
30 <= 75 | qgis1.1 php_mapscript
85 <= 90 | exiv2-devel mingw-libs tcltk-demos fcgi netcdf pdcurses-devel msvcrt
61 <= 90 | gdal-grass iconv qgis-devel qgis1.1 php_mapscript
96 <= 105 | exiv2-devel mingw-libs tcltk-demos fcgi netcdf pdcurses-devel msvcrt gdal-grass iconv
39 <= 105 | qgis-devel qgis1.1 php_mapscript
117 <= 120 | exiv2-devel mingw-libs tcltk-demos fcgi netcdf pdcurses-devel msvcrt gdal-grass iconv qgis-devel qgis1.1
14 <= 120 | php_mapscript
130 <= 135 | exiv2-devel mingw-libs tcltk-demos fcgi netcdf pdcurses-devel msvcrt gdal-grass iconv qgis-devel qgis1.1 php_mapscript
Upvotes: 1
Reputation: 101
A succint variation that adapts the column width to the longest item in that column:
from itertools import zip_longest
def print_columns(obj_to_print, col_length, gutter=2):
columns = []
for i in range(1 + len(obj_to_print) // col_length):
col_items = [str(item) for item in obj_to_print[i*col_length:(i+1)*col_length]]
col_width = len(max(col_items, key=len))
columns.append([f"{item:{col_width}}" for item in col_items])
for row in zip_longest(*columns, fillvalue=""):
print(f"{' ' * gutter}".join(row))
Upvotes: 0
Reputation: 539
For Python3, I used python - How do you split a list into evenly sized chunks? - Stack Overflow to create
def chunkSectionList(listToPrint, columns):
""" separate a list into chunks of n-items """
for i in range(0, len(listToPrint), columns):
yield listToPrint[i:i + columns]
def printSections(listToPrint, columns):
remainder = len(listToPrint) % columns
listToPrint += (columns - remainder) * \
(" ") if remainder != 0 else listToPrint
for sectionsLine in chunkSectionList(listToPrint, columns):
formatStr = columns * '{:<30}'
print(formatStr.format(* sectionsLine))
Upvotes: 0
Reputation: 937
I use IPython internal columnize function
import IPython
foolist = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
'qgis1.1', 'php_mapscript']
foolist_columnized = IPython.utils.text.columnize(foolist)
print(foolist_columnized)
output will look like:
exiv2-devel tcltk-demos netcdf msvcrt iconv qgis1.1
mingw-libs fcgi pdcurses-devel gdal-grass qgis-devel php_mapscript
Upvotes: 3
Reputation: 55
For Python >=3.6, slight update to @JoshuaZastrow's answer using f-strings
and adding clear method to adjust columns
cols = 5
[print(f'{key:20}', end='\t') if (idx + 1) % cols else print(f'{key}') for idx, key in enumerate(list_variable)]
or
cols = 5
for idx, key in enumerate(list_variable):
if (idx + 1) % cols:
print(f'{key:20}', end='\t')
else:
print(f'{key}')
Upvotes: 0
Reputation: 539
this one prints list in separate columns (order is preserved)
from itertools import zip_longest
def ls(items, n_cols=2, pad=30):
if len(items) == 0:
return
total = len(items)
chunk_size = total // n_cols
if chunk_size * n_cols < total:
chunk_size += 1
start = range(0, total, chunk_size)
end = range(chunk_size, total + chunk_size, chunk_size)
groups = (items[s:e] for s, e in zip(start, end))
for group in zip_longest(*groups, fillvalue=''):
template = (' ').join(['%%-%ds' % pad] * len(group))
print(template % group)
usage:
ls([1, 2, 3, 4, 5, 6, 7], n_cols=3, pad=10)
output:
1 4 7
2 5
3 6
note that there may be missing columns if there is not enough number of items, because columns are filled first.
ls([1, 2, 3, 4, 5], n_cols=4)
output:
1 3 5
2 4
Upvotes: 0
Reputation: 1785
Here is a straightforward way. See the inline comments for explanation:
import shutil
import itertools
from functools import reduce
def split_list(lst, ncols):
"""Split list into rows"""
return itertools.zip_longest(
*[lst[i::ncols] for i in range(ncols)], fillvalue=""
)
# -- Alternatively --
# import numpy as np
# array = np.array(lst)
# nrows = array.size / ncols + 1
# return np.array_split(array, int(nrows))
def print_in_columns(lst):
"""Print a list in columns."""
# Find maximum length of a string in colors_list
colsize = reduce(lambda x, y: max(x, len(y)), lst, 0)
# Terminal width
maxcols = shutil.get_terminal_size()[0]
ncols = maxcols / (colsize + 1)
rows = split_list(lst, int(ncols))
print(
# Join rows
"\n".join(
(
# Fill items left justified
" ".join(item.ljust(colsize) for item in row)
for row in rows
)
)
)
Upvotes: 0
Reputation: 2758
I needed to adjust each of the columns. I have implemented this code
def print_sorted_list(data, columns):
if data:
gap = 2
ljusts = {}
for count, item in enumerate(sorted(data), 1):
column = count % columns
ljusts[column] = len(item) if (column not in ljusts) else max(ljusts[column], len(item))
for count, item in enumerate(sorted(data), 1):
print item.ljust(ljusts[count % columns] + gap),
if (count % columns == 0) or (count == len(data)):
print
Example:
foolist = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
'qgis1.1', 'php_mapscript', 'blablablablablablabla', 'fafafafafafa']
print_sorted_list(foolist, 4)
Output:
blablablablablablabla exiv2-devel fafafafafafa fcgi
gdal-grass iconv mingw-libs msvcrt
netcdf pdcurses-devel php_mapscript qgis-devel
qgis1.1 tcltk-demos
Upvotes: 0
Reputation: 11
As an expansion of @Aman below is a function which takes a list of strings and outputs them in columns based on the terminal size.
import os
def column_display(input_list):
'''
Used to create a structured column display based on the users terminal size
input_list : A list of string items which is desired to be displayed
'''
rows, columns = os.popen('stty size', 'r').read().split()
terminal_space_eighth = int(columns)/8
terminal_space_seventh = int(columns)/7
terminal_space_sixth = int(columns)/6
terminal_space_fifth = int(columns)/5
terminal_space_quarter = int(columns)/4
terminal_space_third = int(columns)/3
terminal_space_half = int(columns)/2
longest_string = max(input_list, key=len)
longest_length = len(longest_string) + 1
list_size = len(input_list)
if longest_length > terminal_space_half:
for string in input_list:
print(string)
elif terminal_space_eighth >= longest_length and list_size >= 8:
for a,b,c,d,e,f,g,h in zip(input_list[::8],input_list[1::8],input_list[2::8], input_list[3::8], input_list[4::8], input_list[5::8], input_list[6::8], input_list[7::8]):
column_space = '{:<%s}{:<%s}{:<%s}{:<%s}{:<%s}{:<%s}{:<%s}{:<}' % (longest_length, longest_length, longest_length, longest_length, longest_length, longest_length, longest_length )
output = column_space.format(a,b,c,d,e,f,g,h)
print(output)
elif terminal_space_seventh >= longest_length and list_size >= 7:
for a,b,c,d,e,f,g in zip(input_list[::7],input_list[1::7],input_list[2::7], input_list[3::7], input_list[4::7], input_list[5::7], input_list[6::7]):
column_space = '{:<%s}{:<%s}{:<%s}{:<%s}{:<%s}{:<%s}{:<}' % (longest_length, longest_length, longest_length, longest_length, longest_length, longest_length)
output = column_space.format(a,b,c,d,e,f,g)
print(output)
elif terminal_space_sixth >= longest_length and list_size >= 6:
for a,b,c,d,e,f in zip(input_list[::6],input_list[1::6],input_list[2::6], input_list[3::6], input_list[4::6], input_list[5::6]):
column_space = '{:<%s}{:<%s}{:<%s}{:<%s}{:<%s}{:<}' % (longest_length, longest_length, longest_length, longest_length, longest_length)
output = column_space.format(a,b,c,d,e,f)
print(output)
elif terminal_space_fifth >= longest_length and list_size >= 5:
for a,b,c,d,e in zip(input_list[::5],input_list[1::5],input_list[2::5], input_list[3::5], input_list[4::5]):
column_space = '{:<%s}{:<%s}{:<%s}{:<%s}{:<}' % (longest_length, longest_length, longest_length, longest_length)
output = column_space.format(a,b,c,d,e)
print(output)
elif terminal_space_quarter >= longest_length and list_size >= 4:
for a,b,c,d in zip(input_list[::4],input_list[1::4],input_list[2::4], input_list[3::4]):
column_space = '{:<%s}{:<%s}{:<%s}{:<}' % (longest_length, longest_length, longest_length)
output = column_space.format(a,b,c,d)
print(output)
elif terminal_space_third >= longest_length and list_size >= 3:
for a,b,c in zip(input_list[::3],input_list[1::3],input_list[2::3]):
column_space = '{:<%s}{:<%s}{:<}' % (longest_length, longest_length)
output = column_space.format(a,b,c)
print(output)
elif terminal_space_half >= longest_length and list_size >= 2:
for a,b in zip(input_list[::2],input_list[1::2]):
column_space = '{:<%s}{:<}' % longest_length
output = column_space.format(a,b)
print(output)
As an explanation this does a few different things.
First it gets the number of columns for the current user's terminal using os.popen.
Second it takes the number of columns and divides in half, increasing to eighth. This will be used to compare the longest string in the list to determine the number of columns best suited for this.
Third is the longest string of the list pulled using the build in python function max().
Forth the length of the longest string is taken and then has one added to it for padding. The length of the list is taken as well so that if the list is less than 8 items it will only list the number of items that exist.
Fifth the longest string length is compared to each of the terminal spaces from one column to eight. If the column is greater than or equal to the length then it can be used. For example is the longest string is 10 and the columns divided by eight(terminal_space_eighth) is 8 but columns divided by seven(terminal_space_seventh) is 12, there will be seven columns. There will be seven because the longest string can fit in 12 characters but not in 8 characters.
It's also worth noting the length of the list is taken into consideration to prevent creating more columns than list items.
Sixth is an expansion of the explanation by @Aman : https://stackoverflow.com/a/1524132/11002603
Indexing
Lets let i represent the number determined by terminal size for the sake of this example.
input_list[::i]
This selects element at i. Adding a number at the front such as input_list[1::i]
offsets the starting point(remember python considers 0 a valid number which is why it's not used initially.)
Zipping
Zip is used to create a tuple with elements of a list. For example The output list will look similar to below
zip([string1,string2,string3], [string4,string5, string6], [string7,string8,string9])
output : [(string1,string4,string7), (string2,string5, string8), (string3,string6,string9)]
Using together Depending on the number of columns, the letters are just used to represent a split. So for example if only 5 columns fit in the terminal, the following will be used
for a,b,c,d,e in zip(input_list[::5],input_list[1::5],input_list[2::5], input_list[3::5], input_list[4::5]):
This will take of the tuples created from zipping and store then as a,b,c,d and e variables so we can call them within the loop.
The column space is then used for format each of a,b,c,d and e into respective columns and is where the length of each column is determined. The length is based on the string length determined above.
Upvotes: 0
Reputation: 159
There are tons of answers already, but I will share my solution, which in addition to printing the list into multiple columns, it also chooses the amount of columns dynamically, from the terminal width and the longest string on the list.
import os
cols = os.popen('stty size', 'r').read().split()[1]
def print_multicol(my_list):
max_len = len(max(my_list,key=len)) + 2
ncols = (int(cols) -4 ) / max_len
while my_list:
n = 0
while n < ncols:
if len(my_list) > 0 :
fstring = "{:<"+str(max_len)+"}"
print fstring.format(my_list.pop(0)),
n += 1
print
a_list = "a ab abc abcd abcde b bc bcde bcdef c cde cdef cdfg d de defg"
a_list += "defgh e ef efg efghi efghij f fg fgh fghij fghijk"
print_multicol(a_list.split())
Upvotes: 0
Reputation: 1403
[print('{:20}'.format(key), end='\t') if (idx + 1) % 5 else print(key, end='\n') for idx, key in enumerate(list_variable)]
or
for idx, key in enumerate(list_variable):
if (idx + 1) % 5:
print('{:20}'.format(key), end='\t')
else:
print(key, end='\n')
Upvotes: 1
Reputation: 2261
How about something like this?
def strlistToColumns( strl, maxWidth, spacing=4 ):
longest = max([len(s) for s in strl])
width = longest+spacing
# compute numCols s.t. (numCols-1)*(longest+spacing)+longest < maxWidth
numCols = 1 + (maxWidth-longest)//width
C = range(numCols)
# If len(strl) does not have a multiple of numCols, pad it with empty strings
strl += [""]*(len(strl) % numCols)
numRows = len(strl)/numCols
colString = ''
for r in range(numRows):
colString += "".join(["{"+str(c)+":"+str(width)+"}" \
for c in C]+["\n"]).format(*(strl[numCols*r+c] \
for c in C))
return colString
if __name__ == '__main__':
fruits = ['apple', 'banana', 'cantaloupe', 'durian', 'elderberry', \
'fig', 'grapefruit', 'honeydew', 'indonesian lime', 'jackfruit', \
'kiwi', 'lychee', 'mango', 'orange', 'pomegranate', 'quince', \
'raspberry', 'tangerine', 'ugli fruit', 'watermelon', 'xigua',
'yangmei', 'zinfandel grape']
cols = strlistToColumns( fruits, 80 )
print(cols)
Output
apple banana cantaloupe durian
elderberry fig grapefruit honeydew
indonesian lime jackfruit kiwi lychee
mango orange pomegranate quince
raspberry tangerine ugli fruit watermelon
xigua yangmei zinfandel grape
Upvotes: 1
Reputation: 159
It's useful to allow for uneven columns, without having to know in advance how many columns you can fit:
>>> words = [string.ascii_lowercase] + list(string.ascii_lowercase)
>>> print format_list(words)
abcdefghijklmnopqrstuvwxyz b d f h j l n p r t v x z
a c e g i k m o q s u w y
For your example:
>>> foolist = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi',
... 'netcdf', 'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv',
... 'qgis-devel', 'qgis1.1', 'php_mapscript']
>>> print format_list(foolist, spacing=4, width=31)
exiv2-devel msvcrt
mingw-libs gdal-grass
tcltk-demos iconv
fcgi qgis-devel
netcdf qgis1.1
pdcurses-devel php_mapscript
Here is the code. Note that it also handles words with ANSI color codes (such as from the colorama package) - they won't mess up the column widths.
ansi_pattern = re.compile(r'\x1b\[\d{1,2}m')
def get_nchars(string):
"""Return number of characters, omitting ANSI codes."""
return len(ansi_pattern.sub('', string))
def format_list(items, indent=0, spacing=2, width=79):
"""Return string listing items along columns.
items : sequence
List of items to display that must be directly convertible into
unicode strings. ANSI color codes may be present, and are taken
into account in determining column widths
indent : int
Number of spaces in left margin.
spacing : int
Number of spaces between columns.
width : int
Maximum number of characters per line, including indentation.
"""
if not items:
return u''
# Ensure all items are strings
items = [unicode(item) for item in items]
# Estimate number of columns based on shortest and longest items
minlen = min(get_nchars(item) for item in items)
maxlen = max(get_nchars(item) for item in items)
# Assume one column with longest width, remaining with shortest.
# Use negative numbers for ceiling division.
ncols = 1 - (-(width - indent - maxlen) // (spacing + min(1, minlen)))
ncols = max(1, min(len(items), ncols))
# Reduce number of columns until items fit (or only one column)
while ncols >= 1:
# Determine number of rows by ceiling division
nrows = -(-len(items) // ncols)
# Readjust to avoid empty last column
ncols = -(-len(items) // nrows)
# Split items into columns, and test width
columns = [items[i*nrows:(i+1)*nrows] for i in range(ncols)]
totalwidth = indent - spacing + sum(
spacing + max(get_nchars(item) for item in column)
for column in columns
)
# Stop if columns fit. Otherwise, reduce number of columns and
# try again.
if totalwidth <= width:
break
else:
ncols -= 1
# Pad all items to column width
for i, column in enumerate(columns):
colwidth = max(get_nchars(item) for item in column)
columns[i] = [
item + ' ' * (colwidth - get_nchars(item))
for item in column
]
# Transpose into rows, and return joined rows
rows = list(itertools.izip_longest(*columns, fillvalue=''))
return '\n'.join(
' ' * indent + (u' ' * spacing).join(row).rstrip()
for row in rows
)
Upvotes: 0
Reputation: 11293
I extend an n
column solution to @Aman's answer
def printMultiCol(l, n_cols, buffer_len=5):
"""formats a list of strings, l, into n_cols with a separation of buffer_len"""
if not l: return [] # return if not iterable!
max_l = max(map(len, l))
formatter = '{{:<{max_l}}}'.format(max_l=max_l+buffer_len)*n_cols
zip_me_up = [l[i::n_cols] for i in xrange(n_cols)]
max_zip_l = max(map(len, zip_me_up))
zip_me_up = map(lambda x: x + ['']*(max_zip_l-len(x)), zip_me_up)
return [formatter.format(*undress_me) for undress_me in zip(*zip_me_up)]
Set up the test with random string lengths
import random
list_length = 16
random_strings = [
''.join(random.choice('spameggsbaconbeanssausage')
for x in range(random.randint(1,10)))
for i in xrange(list_length)
]
print 'for 4 columns (equal length cols) ...\n{}'.format(
'\n'.join(printMultiCol(random_strings, 4))
)
print 'for 7 columns (odd length cols) ...\n{}'.format(
'\n'.join(printMultiCol(random_strings, 5))
)
which returns
## -- End pasted text --
for 4 columns (equal length cols) ...
sgsebpasgm assgaesse ossmeagan ebesnagec
mees eeges m gcb
sm pbe bbgaa ganopabnn
bmou asbegu a psoge
for 7 columns (odd length cols) ...
sgsebpasgm assgaesse ossmeagan ebesnagec mees
eeges m gcb sm pbe
bbgaa ganopabnn bmou asbegu a
psoge
Upvotes: 1
Reputation: 43
Inspired by gimel's answer, above.
import math
def list_columns(obj, cols=4, columnwise=True, gap=4):
"""
Print the given list in evenly-spaced columns.
Parameters
----------
obj : list
The list to be printed.
cols : int
The number of columns in which the list should be printed.
columnwise : bool, default=True
If True, the items in the list will be printed column-wise.
If False the items in the list will be printed row-wise.
gap : int
The number of spaces that should separate the longest column
item/s from the next column. This is the effective spacing
between columns based on the maximum len() of the list items.
"""
sobj = [str(item) for item in obj]
if cols > len(sobj): cols = len(sobj)
max_len = max([len(item) for item in sobj])
if columnwise: cols = int(math.ceil(float(len(sobj)) / float(cols)))
plist = [sobj[i: i+cols] for i in range(0, len(sobj), cols)]
if columnwise:
if not len(plist[-1]) == cols:
plist[-1].extend(['']*(len(sobj) - len(plist[-1])))
plist = zip(*plist)
printer = '\n'.join([
''.join([c.ljust(max_len + gap) for c in p])
for p in plist])
print printer
Results (the second one satisfies your request):
>>> list_columns(foolist)
exiv2-devel fcgi msvcrt qgis-devel
mingw-libs netcdf gdal-grass qgis1.1
tcltk-demos pdcurses-devel iconv php_mapscript
>>> list_columns(foolist, cols=2)
exiv2-devel msvcrt
mingw-libs gdal-grass
tcltk-demos iconv
fcgi qgis-devel
netcdf qgis1.1
pdcurses-devel php_mapscript
>>> list_columns(foolist, columnwise=False)
exiv2-devel mingw-libs tcltk-demos fcgi
netcdf pdcurses-devel msvcrt gdal-grass
iconv qgis-devel qgis1.1 php_mapscript
>>> list_columns(foolist, gap=1)
exiv2-devel fcgi msvcrt qgis-devel
mingw-libs netcdf gdal-grass qgis1.1
tcltk-demos pdcurses-devel iconv php_mapscript
Upvotes: 12
Reputation: 71
Found this question as a met almost the same task. And I've created function to print list in multi columns with the number of columns as parameter. Maybe not so elegant as one-liner solutions, but it could be useful for someone.
However, it handles incomplete lists, ex.: it can print list of 11 in 3 rows.
Function splitted for better readability:
def is_printable(my_list):
return len(my_list) > 0
def create_empty_list(columns):
result = []
for num in range(0, columns):
result.append([])
return result
def fill_empty_list(empty_list, my_list, columns):
column_depth = len(my_list) / columns if len(my_list) % columns == 0 else len(my_list) / columns + 1
item_index = 0
for column in range(0, columns):
while len(empty_list[column]) < column_depth:
if item_index < len(my_list):
empty_list[column].append(my_list[item_index])
else:
empty_list[column].append(" ") # last column could be incomplete, fill it with space
item_index += 1
def print_list_in_columns(my_list, columns=1):
if not is_printable(my_list):
print 'Nothing to print, sorry...'
return
column_width = 25 #(in symbols) Also can be calculated automatically
list_to_print = create_empty_list(columns)
fill_empty_list(list_to_print, my_list, columns)
iterators = ["it" + str(i) for i in range(0, columns)]
for iterators in zip(*list_to_print):
print ("".join(str.ljust(i, column_width) for i in iterators))
and the call part:
foolist = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
'qgis1.1', 'php_mapscript']
print_list_in_columns(foolist, 2)
Upvotes: 0
Reputation: 1739
Here's a solution in python 3.4 that automatically detects terminal width and takes it into account. Tested on Linux and Mac.
def column_print(list_to_print, column_width=40):
import os
term_height, term_width = os.popen('stty size', 'r').read().split()
total_columns = int(term_width) // column_width
total_rows = len(list_to_print) // total_columns
# ceil
total_rows = total_rows + 1 if len(list_to_print) % total_columns != 0 else total_rows
format_string = "".join(["{%d:<%ds}" % (c, column_width) \
for c in range(total_columns)])
for row in range(total_rows):
column_items = []
for column in range(total_columns):
# top-down order
list_index = row + column*total_rows
# left-right order
#list_index = row*total_columns + column
if list_index < len(list_to_print):
column_items.append(list_to_print[list_index])
else:
column_items.append("")
print(format_string.format(*column_items))
Upvotes: 0
Reputation: 2008
Here's my solution. (Copy in GitHub gist)
It takes terminal width as input and displays only as many columns that can be fit in it.
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))
Upvotes: 1
Reputation: 47229
This answer uses the same method in the answer by @Aaron Digulla, with slightly more pythonic syntax. It might make some of the above answers easier to understand.
>>> for a,b,c in zip(foolist[::3],foolist[1::3],foolist[2::3]):
>>> print '{:<30}{:<30}{:<}'.format(a,b,c)
exiv2-devel mingw-libs tcltk-demos
fcgi netcdf pdcurses-devel
msvcrt gdal-grass iconv
qgis-devel qgis1.1 php_mapscript
This can be easily adapt to any number of columns or variable columns, which would lead to something like the answer by @gnibbler. The spacing can be adjusted for screen width.
Update: Explanation as requested.
Indexing
foolist[::3]
selects every third element of foolist
. foolist[1::3]
selects every third element, starting at the second element ('1' because python uses zero-indexing).
In [2]: bar = [1,2,3,4,5,6,7,8,9]
In [3]: bar[::3]
Out[3]: [1, 4, 7]
zip
Zipping lists (or other iterables) generates tuples of the elements of the the lists. For example:
In [5]: zip([1,2,3],['a','b','c'],['x','y','z'])
Out[5]: [(1, 'a', 'x'), (2, 'b', 'y'), (3, 'c', 'z')]
together
Putting these ideas together we get our solution:
for a,b,c in zip(foolist[::3],foolist[1::3],foolist[2::3]):
Here we first generate three "slices" of foolist
, each indexed by every-third-element and offset by one. Individually they each contain only a third of the list. Now when we zip these slices and iterate, each iteration gives us three elements of foolist
.
Which is what we wanted:
In [11]: for a,b,c in zip(foolist[::3],foolist[1::3],foolist[2::3]):
....: print a,b,c
Out[11]: exiv2-devel mingw-libs tcltk-demos
fcgi netcdf pdcurses-devel
[etc]
Instead of:
In [12]: for a in foolist:
....: print a
Out[12]: exiv2-devel
mingw-libs
[etc]
Upvotes: 35
Reputation: 328810
Simple:
l = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
'qgis1.1', 'php_mapscript']
if len(l) % 2 != 0:
l.append(" ")
split = len(l)/2
l1 = l[0:split]
l2 = l[split:]
for key, value in zip(l1,l2):
print '%-20s %s' % (key, value) #python <2.6
print "{0:<20s} {1}".format(key, value) #python 2.6+
Upvotes: 12
Reputation: 223152
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)
Upvotes: -1
Reputation: 304463
The way Aaron has done it can work with more than two colums
>>> l = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
... 'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
... 'qgis1.1', 'php_mapscript']
>>> cols = 4
>>> split=[l[i:i+len(l)/cols] for i in range(0,len(l),len(l)/cols)]
>>> for row in zip(*split):
... print "".join(str.ljust(i,20) for i in row)
...
exiv2-devel fcgi msvcrt qgis-devel
mingw-libs netcdf gdal-grass qgis1.1
tcltk-demos pdcurses-devel iconv php_mapscript
Upvotes: 5
Reputation: 86492
See formatting-a-list-of-text-into-columns,
A general solution, handles any number of columns and odd lists. Tab characters separate columns, using generator expressions to save space.
def fmtcols(mylist, cols):
lines = ("\t".join(mylist[i:i+cols]) for i in xrange(0,len(mylist),cols))
return '\n'.join(lines)
Upvotes: 5
Reputation: 304463
If the data is in the format you have provided, it is a little more work
>>> d = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
... 'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
... 'qgis1.1', 'php_mapscript']
>>> print "\n".join("%-20s %s"%(d[i],d[i+len(d)/2]) for i in range(len(d)/2))
exiv2-devel msvcrt
mingw-libs gdal-grass
tcltk-demos iconv
fcgi qgis-devel
netcdf qgis1.1
pdcurses-devel php_mapscript
Upvotes: 3