-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatting_from_before.txt
178 lines (129 loc) · 5.46 KB
/
formatting_from_before.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#############################################
import string
from io import StringIO
import re
def print_2d_list_columns_aligned(list_2d_to_print,
joining_delimiter = " ",
output_as_str_not_list = False,
do_see_the_guts=False,
do_use_example=False):
if do_use_example:
list_2d_to_print = [
["Ah!", "We do have some Camembert",
1533244593020204045868120404042273, "jungle"],
["It's a bit", "runny", "sir", "black"],
["Well,", "as a matter of fact it's", "very runny, sir", 3e24],
["I think it's runnier", "than you", "like it, sir", "no"]
]
##endof: if do_see_the_guts
## Make all elements strings - so we can use len()
list_2d_all_strings = \
[[str(item) for item in row] for row in list_2d_to_print]
if do_see_the_guts:
print()
print(" list_2d_all_strings:")
print(list_2d_all_strings)
print()
##endof: if do_see_the_guts
# We want to find the max string length for each column
#+ We can basically transpose the 2d_list to get the
#+ content of each column
list_of_column_elems_as_tuples = \
[column for column in zip(*list_2d_all_strings)]
if do_see_the_guts:
print()
print(" list_of_column_elems_as_tuples:")
print(list_of_column_elems_as_tuples)
print()
##endof: if do_see_the_guts
## find the max string length for each tuple (each column)
list_of_max_str_len_by_column = \
[max([len(strng) for strng in tpl])
for tpl in list_of_column_elems_as_tuples]
# -v- gives array with elements being each longest string
#[max([strng for strng in tpl], key=len) for tpl in list_of_column_elems_as_tuples]
# -v- 2d array with strings
#[[strng for strng in tpl] for tpl in list_of_column_elems_as_tuples]
if do_see_the_guts:
print()
print(" list_of_max_str_len_by_column:")
print(list_of_max_str_len_by_column)
print()
##endof: if do_see_the_guts
# Create a formatter for each row
if not output_as_str_not_list:
joining_delimiter = " ," + joining_delimiter
fmt_str = \
joining_delimiter.join('{{:{}}}'.format(max_len)
for max_len in list_of_max_str_len_by_column)
if not output_as_str_not_list:
fmt_str = "[ " + fmt_str + " ],"
##endof: if output_as_str_not_list
if do_see_the_guts:
print()
print(" fmt_str:")
print(fmt_str)
print()
##endof: if do_see_the_guts
# Get a string for each row, formatted correctly
list_of_formatted_row_strings = \
[fmt_str.format(*row) for row in list_2d_all_strings]
if do_see_the_guts:
print()
print(" list_of_formatted_row_strings:")
print(list_of_formatted_row_strings)
print()
##endof: if do_see_the_guts
if output_as_str_not_list:
aligned_table_to_return = '\n'.join(list_of_formatted_row_strings)
print(aligned_table_to_return)
#return aligned_table_to_return
##endof: if output_as_str_not_list
s = StringIO()
print(*list_of_formatted_row_strings, file=s)
output_table_raw = s.getvalue()
output_table_raw = re.sub(r"^\[", r"[[", output_table_raw)
output_table_raw = re.sub(r"\],$", r" ]]", output_table_raw)
output_table_raw = re.sub(r"\],", r" ],", output_table_raw)
aligned_table_to_return = output_table_raw.replace(r"], [", "],\n [")
print(aligned_table_to_return)
#return aligned_table_to_return
##endof: print_2d_list_colunns_aligned(<params>)
#############################################
matrix = [
["Ah!", "We do have some Camembert", 1533244593020204045868120404042273, "jungle"],
["It's a bit", "runny", "sir", "black"],
["Well,", "as a matter of fact it's", "very runny, sir", 3e24],
["I think it's runnier", "than you", "like it, sir", "no"]
]
print_2d_list_columns_aligned(matrix, do_see_the_guts=True)
###############################################
matrix = [
["Ah!", "We do have some Camembert", 1533244593020204045868120404042273, "jungle"],
["It's a bit", "runny", "sir", "black"],
["Well,", "as a matter of fact it's", "very runny, sir", 3e24],
["I think it's runnier", "than you", "like it, sir", "no"]
]
##############################################
s = [[str(e) for e in row] for row in matrix]
s
###################################################
str(zip(*s))
###################################################
lens = [max(map(len,col)) for col in zip(*s)]
lens
###################################################
[print(str(col)) for col in zip(*s)]
###################################################
fmt = ' '.join('{{:{}}}'.format(x) for x in lens)
fmt
###################################################
this_table = [fmt.format(*row) for row in s]
this_table
###################################################
print('\n'.join(this_table))
###################################################
https://stackoverflow.com/a/13214945/6505499
https://stackoverflow.com/questions/44287965/trying-to-confirm-average-pooling-is-equal-to-dropping-high-frequency-fourier-co?noredirect=1&lq=1
https://stackoverflow.com/a/31567111/6505499
https://stackoverflow.com/questions/27388868/printing-out-a-2d-list-like-a-table-with-python?rq=3