Created
January 22, 2016 22:37
-
-
Save danielcodes/9840c91f8e94e5177aec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#table formatting example | |
# lists to create table | |
header = ['First name', 'Last name'] | |
first = ['Daniel', 'Ronnie', 'Vanessa'] | |
last = ['Chia', 'Hernandez', 'Luka'] | |
# define variables | |
col_count = 2 | |
col_widths = [10, 9] | |
formatters = ['{:10}', '{:9}'] | |
print('\n') | |
# start building the list | |
result = [] | |
# append header | |
print('====== this is a first append') | |
result.append( | |
formatters[i].format(header[i]) | |
for i in range(col_count)) | |
print(result) | |
print('\n') | |
# append line separator | |
print('====== this is a second append') | |
result.append( | |
('=' * col_widths[i] | |
for i in range(col_count))) | |
for i in result: | |
print(i) | |
print('\n') | |
# append data rows | |
print('====== this is a third append') | |
data = (first, last) #unpacked with * | |
for row in zip(*data): | |
result.append( | |
[formatters[i].format(row[i]) | |
for i in range(col_count)]) | |
for i in result: | |
print(i) | |
print('\n') | |
#join the each row with spaces | |
print('====== Join the collections inside with spaces') | |
result = (' '.join(r) for r in result) | |
print(result) | |
print('\n') | |
print('====== Final result, join collections with new lines') | |
print('\n'.join(result)) | |
print('\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment