r/inventwithpython • u/[deleted] • Feb 10 '16
Automate The Boring Stuff - Chapter 6: Table Printer
It was quite challenging for me to do this one; I still feel quite lost with nested loops. Eventually I could do it as follows, but I think it's too much code.
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printTable():
colWidths = [0] * len(tableData)
x = 0
y = 0
z = 0
for c in tableData:
for w in tableData[0]:
if len(w) > x:
x = len(w)
colWidths[0] = x
for w in tableData[1]:
if len(w) > y:
y = len(w)
colWidths[1] = y
for w in tableData[2]:
if len(w) > z:
z = len(w)
colWidths[2] = z
colWidths.sort()
maxWidth = colWidths[-1]
column = []
c = 0
for i in range(len(tableData[0])):
for j in range(len(tableData)):
column.append(tableData[j][i])
for i in tableData[0]:
for j in tableData:
print(column[c].rjust(maxWidth), end='')
c += 1
print()
printTable()It works, but I'm sure it should be much shorter. What am I not getting?
2
u/fenpy Feb 12 '16 edited Feb 12 '16
This is pretty amazing I must say. I really like how you solved this problem, because it shows that you can think like programer, at least I think you can. I say this because I would never come up with solution like this, and I don't think that's good. I'm noob at programming and Pyton, and I'm on that chapter too, and solved it in just couple lines of code, but I really do like your approach.
This was my solution.
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
for a, b, c in zip(tableData[0], tableData[1], tableData[2]):
print(a.rjust(10), b.rjust(10), c.rjust(10))Just before i got to this problem i was watching The newboston tutorials about zip, and figured out that I can use it here in this example.
Since we have lists inside list in this example, we can use zip function to combine multiple lists and iterate over it.
1
Feb 12 '16
Thanks for the props! Actually I was feeling a bit down because of the amount of code and struggle it took me to complete this exercise.
I really like how you did it. All my work could have been done only in two lines. Had no idea about the zip function but I'm definitely going to look into it.
6
u/ParalysedBeaver May 22 '16 edited Oct 05 '17
My solution.
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('')Edited to add correct indenting.
2
u/PM_Me_Your_Picks Jul 29 '16
This needs more love. Very clean and only using material that has been introduced in the book up to this point. Nice.
2
u/toni9487 May 10 '16 edited May 10 '16
Hell, I've finally done it, too. Took me quite a while! I must admit, that I took some inspiration from this post. So here is my code:
table = [['apples', 'oranges', 'strawberries', 'cherries', 'bananas'],
['Alice', 'Bob', 'Carol', 'David', 'Tom'],
['dogs', 'cats', 'elephants', 'goose', 'cows'],
['cars', 'boats', 'bycicles', 'planes', 'trains'],
['x', 'xyz', 'xy', 'xyzabcd', 'x']]
def getColWidth(tableData):
# create list for colWidth and store the max for each line in maxCol
colWidths = [0] * len(tableData[0])
maxCol = []
for i in range(len(tableData)):
for item in tableData[i]:
del colWidths[0]
colWidths.append(len(item))
maxCol.append(max(colWidths))
return maxCol
def printTable(tableData, maxColWidth):
print("The new and aligned table:" + "\n")
# create items list
items = [0] * len(tableData)
# transpose tableData in list "items" and print each item aligned to the right
# according to the length of the longest word in each column
for item in tableData:
del items[0]
items.append(item)
for i in range(len(items[0])):
for j in range(len(items)):
print(items[j][i].rjust(maxColWidth[j], "."), end=" ")
print()
printTable(table, getColWidth(table))1
u/darthshatner Feb 23 '16
Hey!
Perhaps I'm missing something, but I really don't see the point of Sweigart's recommendation that we create a new list via:
colWidth = [0] * len(tableData)In order to be properly formatted, the column width in the final table needs to be >= to the longest string in the entire nested list. Only one value is needed. Here's what I did, and it seemed to work pretty well.
I'm still very new at this, so if anyone spots any potential problems, please let me know!
#! python3
# printTable.py - takes a list of lists of strings and presents it in a
# well-organized table.
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(list):
colWidth = 0
for l in range(len(list)):
for s in range(len(list[l])):
if len(list[l][s]) > colWidth:
colWidth = len(list[l][s])
for l in range(len(list)):
for s in range(len(list[l])):
print(list[l][s].rjust(colWidth + 2), end = '')
print()
printTable(tableData)Instead of creating a list of column widths, I'm just using a single integer value that gets set equal to the length of the longest string found in tableData.
/edit grammar
1
u/nxh058 Apr 15 '16
The challenge was to make each column the correct width for the longest word in that column...
1
u/HeiseSays Oct 16 '22
I thought I'd share my code for this problem. I wanted to be able to alter the size of the data library and have the program still work.
# Write your code here :-)
tableData = [
["apples", "oranges", "cherries", "banana", "hello"],
["Alice", "Bob", "Carol", "David", "hello"],
["dogs", "cats", "moose", "goose", "hello"],
["star", "news", "hope", "hello", "hello"],
["star", "news", "hope", "hello", "hello"],
["star", "news", "hope", "hello", "hello"],
["star", "news", "hope", "hello", "hello"],
["star", "news", "hope", "hello", "hello"],
]
# variable
orHeight = len(tableData)
orWidth = len(tableData[0])
dict = {}
def tableHeight(data):
len(data)
print(f"The original height is {orHeight}")
print(f"The original width is {orWidth}")
print("\n")
# Entry Length
def determineColumnWidth(data):
global orWidth
col1 = 0
m = 0
x = 0
n = 0
while m < orHeight:
# print(m)
for i in data[m]:
if len(i) >= col1:
col1 = len(i)
# print(i)
dict[n] = col1
n += 1
m += 1
col1 = 0
# Printing the Data
def printData(data):
global dict
for y in range(orWidth):
i = 0
for x in range(orHeight):
print(f"{tableData [x][y].rjust(int(dict.get(i)))}", end=" ")
i += 1
print("\n")
determineColumnWidth(tableData)
printData(tableData)
print(dict)
0
Feb 21 '16
[deleted]
2
u/WhyUMadSon Apr 03 '16
I really don't understand this approach, can you please explain the usage of two nested loops for the list comprehensions?
0
Mar 20 '16
[deleted]
1
u/nxh058 Apr 15 '16
print(tableData[j][x].rjust(colWidths[0]),end = "")
Look at the order of [j],[x] in the print command. This is saying print position [x] of list [j], then iterate to the next list and print position [x], for example when x is 0 it prints the first word of each list.
Switch x and j around and it will print each word in the first list and then move to the next list.
A problem I noticed with this line is that the rjust command is always taking the value from colWidths[0] over every iteration. The challenge was to make each column the correct width for the longest word in that column...
2
u/mirthcontrol Feb 11 '16
One thing that helps clean this up is using enumerate() when you need to iterate through indices, like so: