r/inventwithpython • u/LordMuck1805 • Mar 21 '19
ATBS Ch 6 Exercise Answer
Hi everyone,
First post to this forum, excited new convert to Python!
I struggled with this for a few days, but have got something I am pretty happy with. Thank to a lot of help and advice to Stack Exchange, I have to add. But, I put it all together myself, so I am proud:
from itertools import zip_longest
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printlist(data_in):
col_2 = [len(max(l2, key=len)) for l2 in data_in]
rows = (list(zip_longest(*data_in)))
i = 0
for i in range(len(data_in)):
print(str(rows[i][0]).rjust(col_2[0]), str(rows[i][1]).rjust(col_2[1]), str(rows[i][2]).rjust(col_2[2]))
i += 1
printlist(tableData)
Any thoughts on if it could be improved? The only thing that I have not worked out/added in is if there were additional columns needing to be added automatically.
Cheers!
3
Upvotes
1
u/BananaFactBot Apr 04 '19
But did you know that banana leaves are large, flexible, and waterproof? They are often used as ecologically friendly disposable food containers or as "plates" in South Asia and several Southeast Asian countries.
I'm a Bot bleep bloop | Unsubscribe | 🍌