Table Printer excercise
up vote
3
down vote
favorite
I'm new to Python and I came to the following excercise:
Write a function named printTable() that takes a list of lists of
strings and displays it in a well-organized table with each column
right-justified. Assume that all the inner lists will contain the same
number of strings. For example, the value could look like this:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
Your printTable() function would print the following:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
My solution is this:
table_printer.py
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(tableData):
"""
Print table neatly formatted:
e.g:
[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
becomes:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
"""
# make list of ints to store later max len element of each list
colWidths = [0] * len(tableData)
# Store maxlen of each list
i = 0
while i < len(tableData):
colWidths[i] = len(max(tableData[i], key=len))
i = i + 1
# Print formatted
for x in range(len(tableData[0])):
for y in range(len(colWidths)):
print(tableData[y][x].rjust(colWidths[y]), end=' ')
print(end='n')
printTable(tableData)
I wonder if this is a good solution or if there is an easier/better way. It took me quite some time to come up with a solution. Still I feel its probaly not very elegant. Maybe I'm overcomplicating it because I came from C/C++ where you oftenly have to do stuff by hand.
I read that it's often not a good idea in python to write loops like in other languages with explicit indices (what I basically did here). Are there any alternatives?
python beginner strings formatting
add a comment |
up vote
3
down vote
favorite
I'm new to Python and I came to the following excercise:
Write a function named printTable() that takes a list of lists of
strings and displays it in a well-organized table with each column
right-justified. Assume that all the inner lists will contain the same
number of strings. For example, the value could look like this:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
Your printTable() function would print the following:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
My solution is this:
table_printer.py
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(tableData):
"""
Print table neatly formatted:
e.g:
[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
becomes:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
"""
# make list of ints to store later max len element of each list
colWidths = [0] * len(tableData)
# Store maxlen of each list
i = 0
while i < len(tableData):
colWidths[i] = len(max(tableData[i], key=len))
i = i + 1
# Print formatted
for x in range(len(tableData[0])):
for y in range(len(colWidths)):
print(tableData[y][x].rjust(colWidths[y]), end=' ')
print(end='n')
printTable(tableData)
I wonder if this is a good solution or if there is an easier/better way. It took me quite some time to come up with a solution. Still I feel its probaly not very elegant. Maybe I'm overcomplicating it because I came from C/C++ where you oftenly have to do stuff by hand.
I read that it's often not a good idea in python to write loops like in other languages with explicit indices (what I basically did here). Are there any alternatives?
python beginner strings formatting
You can compute the maximal length on each row bynp.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
which returns[8 5 5]
. These numbers will be used to format the strings.
– Sigur
Nov 23 at 2:11
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm new to Python and I came to the following excercise:
Write a function named printTable() that takes a list of lists of
strings and displays it in a well-organized table with each column
right-justified. Assume that all the inner lists will contain the same
number of strings. For example, the value could look like this:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
Your printTable() function would print the following:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
My solution is this:
table_printer.py
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(tableData):
"""
Print table neatly formatted:
e.g:
[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
becomes:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
"""
# make list of ints to store later max len element of each list
colWidths = [0] * len(tableData)
# Store maxlen of each list
i = 0
while i < len(tableData):
colWidths[i] = len(max(tableData[i], key=len))
i = i + 1
# Print formatted
for x in range(len(tableData[0])):
for y in range(len(colWidths)):
print(tableData[y][x].rjust(colWidths[y]), end=' ')
print(end='n')
printTable(tableData)
I wonder if this is a good solution or if there is an easier/better way. It took me quite some time to come up with a solution. Still I feel its probaly not very elegant. Maybe I'm overcomplicating it because I came from C/C++ where you oftenly have to do stuff by hand.
I read that it's often not a good idea in python to write loops like in other languages with explicit indices (what I basically did here). Are there any alternatives?
python beginner strings formatting
I'm new to Python and I came to the following excercise:
Write a function named printTable() that takes a list of lists of
strings and displays it in a well-organized table with each column
right-justified. Assume that all the inner lists will contain the same
number of strings. For example, the value could look like this:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
Your printTable() function would print the following:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
My solution is this:
table_printer.py
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printTable(tableData):
"""
Print table neatly formatted:
e.g:
[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
becomes:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
"""
# make list of ints to store later max len element of each list
colWidths = [0] * len(tableData)
# Store maxlen of each list
i = 0
while i < len(tableData):
colWidths[i] = len(max(tableData[i], key=len))
i = i + 1
# Print formatted
for x in range(len(tableData[0])):
for y in range(len(colWidths)):
print(tableData[y][x].rjust(colWidths[y]), end=' ')
print(end='n')
printTable(tableData)
I wonder if this is a good solution or if there is an easier/better way. It took me quite some time to come up with a solution. Still I feel its probaly not very elegant. Maybe I'm overcomplicating it because I came from C/C++ where you oftenly have to do stuff by hand.
I read that it's often not a good idea in python to write loops like in other languages with explicit indices (what I basically did here). Are there any alternatives?
python beginner strings formatting
python beginner strings formatting
edited yesterday
Neves4
428
428
asked Nov 22 at 19:59
Sandro4912
686121
686121
You can compute the maximal length on each row bynp.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
which returns[8 5 5]
. These numbers will be used to format the strings.
– Sigur
Nov 23 at 2:11
add a comment |
You can compute the maximal length on each row bynp.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
which returns[8 5 5]
. These numbers will be used to format the strings.
– Sigur
Nov 23 at 2:11
You can compute the maximal length on each row by
np.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
which returns [8 5 5]
. These numbers will be used to format the strings.– Sigur
Nov 23 at 2:11
You can compute the maximal length on each row by
np.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
which returns [8 5 5]
. These numbers will be used to format the strings.– Sigur
Nov 23 at 2:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Here is my proposal. It is shorter than OP's solution, specially to compute the length of each word to be used in the format procedure while printing.
In a single line, we obtain a 1D array with maximal lengths.
import numpy as np
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
max_len = np.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
for col in range(len(tableData[0])):
for i in range(len(tableData)):
print ("{:>%d}" % max_len[i]).format(tableData[i][col]),
print ""
Output
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
Hello. While the solution is a nice one, your answer just provides and alternative to OP's code which is not what this site is about. Please expand your answer and explain how you're improving OP's solution.
– яүυк
yesterday
@яүυк, hello. Since my knowledge is not big, I tried to improve it. To be true, my code is shorter but I am not sure if it is more efficient. What do you think?
– Sigur
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Here is my proposal. It is shorter than OP's solution, specially to compute the length of each word to be used in the format procedure while printing.
In a single line, we obtain a 1D array with maximal lengths.
import numpy as np
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
max_len = np.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
for col in range(len(tableData[0])):
for i in range(len(tableData)):
print ("{:>%d}" % max_len[i]).format(tableData[i][col]),
print ""
Output
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
Hello. While the solution is a nice one, your answer just provides and alternative to OP's code which is not what this site is about. Please expand your answer and explain how you're improving OP's solution.
– яүυк
yesterday
@яүυк, hello. Since my knowledge is not big, I tried to improve it. To be true, my code is shorter but I am not sure if it is more efficient. What do you think?
– Sigur
yesterday
add a comment |
up vote
1
down vote
Here is my proposal. It is shorter than OP's solution, specially to compute the length of each word to be used in the format procedure while printing.
In a single line, we obtain a 1D array with maximal lengths.
import numpy as np
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
max_len = np.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
for col in range(len(tableData[0])):
for i in range(len(tableData)):
print ("{:>%d}" % max_len[i]).format(tableData[i][col]),
print ""
Output
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
Hello. While the solution is a nice one, your answer just provides and alternative to OP's code which is not what this site is about. Please expand your answer and explain how you're improving OP's solution.
– яүυк
yesterday
@яүυк, hello. Since my knowledge is not big, I tried to improve it. To be true, my code is shorter but I am not sure if it is more efficient. What do you think?
– Sigur
yesterday
add a comment |
up vote
1
down vote
up vote
1
down vote
Here is my proposal. It is shorter than OP's solution, specially to compute the length of each word to be used in the format procedure while printing.
In a single line, we obtain a 1D array with maximal lengths.
import numpy as np
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
max_len = np.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
for col in range(len(tableData[0])):
for i in range(len(tableData)):
print ("{:>%d}" % max_len[i]).format(tableData[i][col]),
print ""
Output
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
Here is my proposal. It is shorter than OP's solution, specially to compute the length of each word to be used in the format procedure while printing.
In a single line, we obtain a 1D array with maximal lengths.
import numpy as np
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
max_len = np.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
for col in range(len(tableData[0])):
for i in range(len(tableData)):
print ("{:>%d}" % max_len[i]).format(tableData[i][col]),
print ""
Output
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
edited yesterday
answered Nov 23 at 2:12
Sigur
17810
17810
Hello. While the solution is a nice one, your answer just provides and alternative to OP's code which is not what this site is about. Please expand your answer and explain how you're improving OP's solution.
– яүυк
yesterday
@яүυк, hello. Since my knowledge is not big, I tried to improve it. To be true, my code is shorter but I am not sure if it is more efficient. What do you think?
– Sigur
yesterday
add a comment |
Hello. While the solution is a nice one, your answer just provides and alternative to OP's code which is not what this site is about. Please expand your answer and explain how you're improving OP's solution.
– яүυк
yesterday
@яүυк, hello. Since my knowledge is not big, I tried to improve it. To be true, my code is shorter but I am not sure if it is more efficient. What do you think?
– Sigur
yesterday
Hello. While the solution is a nice one, your answer just provides and alternative to OP's code which is not what this site is about. Please expand your answer and explain how you're improving OP's solution.
– яүυк
yesterday
Hello. While the solution is a nice one, your answer just provides and alternative to OP's code which is not what this site is about. Please expand your answer and explain how you're improving OP's solution.
– яүυк
yesterday
@яүυк, hello. Since my knowledge is not big, I tried to improve it. To be true, my code is shorter but I am not sure if it is more efficient. What do you think?
– Sigur
yesterday
@яүυк, hello. Since my knowledge is not big, I tried to improve it. To be true, my code is shorter but I am not sure if it is more efficient. What do you think?
– Sigur
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208246%2ftable-printer-excercise%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can compute the maximal length on each row by
np.array([np.array(max([len(xii) for xii in xi])) for xi in tableData])
which returns[8 5 5]
. These numbers will be used to format the strings.– Sigur
Nov 23 at 2:11