Reputation: 1395
I've been looking into how to print data into columns. I haven't been able to find an elegant way to do this with either a dictionary (keys) or a list (taking keys from dictionary into list).
Ive looked into iterating over each key and printing that, but that doesn't work as you can't use mapping. I've tried using a list and printing each item from the list using string formatting
but as you'd imagine I get back each character with a space from each list item, I can't seem to use .join
. The closest I have been able to get to what I'd like is the answer from Aaron Digulla
here. However this does not print the list items in alphabetical order. I cant believe there is'nt a simple elegant way to do this?
Method from answer discussed above
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 "{0:<20s} {1}".format(key, value)
Upvotes: 1
Views: 347
Reputation: 56644
Here is a somewhat more generic version which lets you specify the number of columns:
def print_in_columns(iterable, cols=2, col_width=20, key=None, reverse=False):
# get items in output order
items = sorted(iterable, key=key, reverse=reverse)
# calculate number of output rows, and pad as needed
rows = (len(items) + cols - 1) // cols
pad = rows * cols - len(items)
items.extend("" for _ in range(pad))
# prepare output template
item_fmt = "{{:{}s}}".format(col_width)
row_fmt = " ".join(item_fmt for _ in range(cols))
# print by row
for r in range(rows):
print(row_fmt.format(*(items[r::rows])))
which used like
files = [
'exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
'qgis1.1', 'php_mapscript'
]
print_in_columns(files, cols=4, col_width=16)
produces
exiv2-devel iconv netcdf qgis-devel
fcgi mingw-libs pdcurses-devel qgis1.1
gdal-grass msvcrt php_mapscript tcltk-demos
Upvotes: 0
Reputation: 1968
You can use sort, try:
l = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
'pdcurses-devel', 'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
'qgis1.1', 'php_mapscript']
l.sort()# Alphabetizes l
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 "{0:<20s} {1}".format(key, value)
Upvotes: 2