-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintTable.py
22 lines (18 loc) · 972 Bytes
/
PrintTable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def printTable(inputList):
# initialize the list "colWidths" with zeroes equal to the length of the input list
colWidths = [0] * len(inputList)
# iterate over the input list to find the longest word in each inner list
# if its larger than the current value, set it as the new value
for i in range(len(inputList)):
for j in range(len(inputList[i])):
if len(inputList[i][j]) > colWidths[i]:
colWidths[i] = len(inputList[i][j])
# assuming each inner list is the same length as the first, iterate over the input list
# printing the x value from each inner list, right justifed to its corresponding value
# in colWidths
for x in range(len(inputList[0])):
for y in range(len(inputList)):
print(inputList[y][x].rjust(colWidths[y]), end = ' ')
print('')
tabledata = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']]
printTable(tabledata)