-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatting_2d_lists.txt
362 lines (269 loc) · 11.1 KB
/
formatting_2d_lists.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import string
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
fmt_str = \
joining_delimiter.join('{{:{}}}'.format(max_len)
for max_len in list_of_max_str_len_by_column)
fmt_str = "[" + fmt_str + "]"
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
##endof: print_2d_list_colunns_aligned(<params>)
print_2d_list_columns_aligned(matrix, do_see_the_guts=True)
#print(table_version_both)
#mx = max((len(str(ele)) for sub in myArray for ele in sub))
################################################################
################################################################
# Now so close!
################################################################
################################################################
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)
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>)
print_2d_list_columns_aligned(matrix, do_see_the_guts=True)
#print(table_version_both)
#mx = max((len(str(ele)) for sub in myArray for ele in sub))
#####################################################
#####################################################
# Even closer
#####################################################
#####################################################
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"\g<1> ],", 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>)
print_2d_list_columns_aligned(matrix, do_see_the_guts=True)
#print(table_version_both)
#mx = max((len(str(ele)) for sub in myArray for ele in sub))