Reputation: 257
So, I'm still sort of new to programming, and I'm trying to format the output of some arrays in Python. I'm finding it hard to wrap my head around some of the aspects of formatting. I have a few arrays that I want to print, in the format of a table.
headings = ["Name", "Age", "Favourite Colour"]
names = ["Barry", "Eustace", "Clarence", "Razputin", "Harvey"]
age = [39, 83, 90, 15, 23]
favouriteColour = ["Green", "Baby Pink", "Sky Blue", "Orange", "Crimson"]
I want the output to look like this: (where the column widths are a little more than the max length in that column)
Name Age Favourite Colour
Barry 39 Green
Eustace 83 Baby Pink
Clarence 90 Sky Blue
Razputin 15 Orange
Harvey 23 Crimson
I tried to do this:
mergeArr = [headings, name, age, favouriteColour]
but (I think) that won't print the headings in the right place?
I tried this:
mergeArr = [name, age, favouriteColour]
col_width = max(len(str(element)) for row in merge for element in row) + 2
for row in merge:
print ("".join(str(element).ljust(col_width) for element in row))
but that prints the data of each object in columns, rather than rows.
Help is appreciated! Thanks.
Upvotes: 2
Views: 3193
Reputation: 13274
Just adding the extra formatting:
ll = [headings] + list(zip(names, age, favouriteColour))
for l in ll:
print("{:<10}\t{:<2}\t{:<16}".format(*l))
# Name Age Favourite Colour
# Barry 39 Green
# Eustace 83 Baby Pink
# Clarence 90 Sky Blue
# Razputin 15 Orange
# Harvey 23 Crimson
The parts in the curly braces are part of python's new character formatting features, while the TABs serve as delimiters. In sum, the .format()
method looks for those curly braces inside the string part to determine what values inside the container l
go where and how those values should be formatted. For example, in the case of the headers, the following is what's happening:
headings = ["Name", "Age", "Favourite Colour"]
print("{:<10}\t{:<3}\t{:<16}".format(*headings))
We use the asterisk (*
) in front of the list to unpack
the elements inside that list.
The first curly brace is for the string "Name", and it is formatted with :<10
which means that it is adjusted to the left and padded with extra space characters, if the length of the string is less than 10. In essence, it will print all characters in a given string and add extra spaces to the right of that string.
The second curly brace is for "Age" and is formatted with :<3
.
The third curly brace is for "Favourite Colour" and is formatted with :<16
.
All those strings are delimited with the TAB character.
The combination of the above steps inside the print function yields:
# Name Age Favourite Colour
I hope this proves useful.
Upvotes: 1
Reputation: 9617
You'd print the heading on its own (the one with name, age, favourite colour).
Then you use the code you have, but with:
rows = zip(name, age, favouriteColour)
for row in rows...
You might also look into the tabulate
package for nicely formatted tables.
Upvotes: 3
Reputation: 1654
Jacob Krall is perfectly correct about using zip
to combine your lists. Once you've done that, though, if you want your columns to align nicely (assuming you are using Python 3.x) then take a look at the .format()
method which is available with strings and as part of the Python print
function. This allows you to specify field widths in your output.
Upvotes: 0
Reputation: 28825
Use zip(*iterables):
print(heading)
for row in zip(names, age, favouriteColour):
print(row) # formatting is up to you :)
Upvotes: 0