Automate the Boring Stuff Chapter 6 Table Printer Almost Done
This is how I did.
For the first part of the code I just used the hint they give to us.
In Chapter 4 / Practice Project / Character Picture Grid we've learned how to "rotate" and then print a list of lists. It was useful for the second part of my code.
#!/usr/bin/python3
# you can think of x and y as coordinates
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(table):
# create a new list of 3 "0" values: one for each list in tableData
colWidths = [0] * len(table)
# search for the longest string in each list of tableData
# and put the numbers of characters in the new list
for y in range(len(table)):
for x in table[y]:
if colWidths[y] < len(x):
colWidths[y] = len(x)
# "rotate" and print the list of lists
for x in range(len(table[0])) :
for y in range(len(table)) :
print(table[y][x].rjust(colWidths[y]), end = ' ')
print()
x += 1
printTable(tableData)
Here's an alternate method that perhaps you could apply to your own code. I first took tableData
and sorted it out into a dictionary so it's easier to work with. After that I found the longest list in terms of characters. This allows us to know how far over the shorter lists should go. Finally, I printed out each lists adding spaces in front of the shorter ones based on the difference from the longest.
# orginal data
tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
# empty dictonary for sorting the data
newTable = {0:[], 1:[], 2:[], 3:[]}
# iterate through each list in tableData
for li in tableData:
for i in range(len(li)):
# put each item of tableData into newTable by index
newTable[i].append(li[i])
# determine the longest list by number of total characters
# for instance ['apples', 'Alice', 'dogs'] would be 15 characters
# we will start with longest being zero at the start
longest = 0
# iterate through newTable
# for example the first key:value will be 0:['apples', 'Alice', 'dogs']
# we only really care about the value (the list) in this case
for key, value in newTable.items():
# determine the total characters in each list
# so effectively len('applesAlicedogs') for the first list
length = len(''.join(value))
# if the length is the longest length so far,
# make that equal longest
if length > longest:
longest = length
# we will loop through the newTable one last time
# printing spaces infront of each list equal to the difference
# between the length of the longest list and length of the current list
# this way it's all nice and tidy to the right
for key, value in newTable.items():
print(' ' * (longest - len(''.join(value))) + ' '.join(value))
Here you go young padawan:
tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
maxlen = 0
for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
maxlen = max(len(fruit) + len (name) + len (animal), maxlen)
for fruit,name,animal in zip(tableData[0], tableData[1], tableData[2]):
length = len(fruit) + len (name) + len (animal)
print ((' ' * (maxlen - length)) + fruit, name, animal)
Looping to determine maxlen is probably not optimal, copypasting was just the quickest thing that came to my mind.