10 examples of 'python print matrix as table' in Python

Every line of 'python print matrix as table' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
42def print_table(table: List[List[str]]):
43 """ Print the lists with evenly spaced columns """
44
45 # print while padding each column to the max column length
46 col_lens = [0] * len(table[0])
47 for row in table:
48 for i,cell in enumerate(row):
49 col_lens[i] = max(len(cell), col_lens[i])
50
51 formats = ["{0:<%d}" % x for x in col_lens]
52 for row in table:
53 print(" ".join(formats[i].format(row[i]) for i in range(len(row))))
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

103def printMatrix(matrixP): #fancy matrices renderer for debugging
104 for row in matrixP:
105 for val in row:
106 print '{:4}'.format(val),
107 print
13def print_table(table):
14 col_width = [min(max(len(x) for x in col), 35) for col in zip(*table)]
15 # print(col_width)
16 for idx, line in enumerate(table):
17 if idx == 1: # first line after table head
18 print("-" * (sum(col_width) + 3 * len(col_width) + 1))
19 print(
20 "| "
21 + " | ".join("{:{}}".format(str(x), col_width[i]) for i, x in enumerate(line))
22 + " |"
23 )
124def printmatrix(list):
125 """
126 Prints out a 2-dimensional list cleanly.
127 This is useful for debugging.
128
129 PARAMETERS
130 list - the 2D-list to display
131 """
132 # determine maximum length
133 maxlen = 0
134 colcount = len(list[0])
135 for col in list:
136 for cell in col:
137 maxlen = max(len(str(cell)), maxlen)
138 # print data
139 format = " %%%is |" % maxlen
140 format = "|" + format*colcount
141 for row in list:
142 print(format % tuple(row))
49def makeTable(values, height, width):
50 lines = [[] for i in range(height + 1)]
51 firstRowString = "|{}" + "+{}" * (len(values) - 1) + '|'
52 rowString = "|{}" * len(values) + '|'
53 cWidth = (width // len(values)) - 1
54 cRemainder = width % len(values) - 1
55 for title, rows in values.items():
56 if cRemainder > 0:
57 heading = "{1:-^{0}}".format(cWidth + 1, title)
58 cRemainder -= 1
59 elif cRemainder < 0:
60 heading = "{1:-^{0}}".format(cWidth - 1, title)
61 cRemainder += 1
62 else:
63 heading = "{1:-^{0}}".format(cWidth, title)
64 hWidth = len(heading)
65 lines[0].append(heading)
66 if len(rows) < height:
67 for i in range(height - len(rows)):
68 rows.append(('NA', -1))
69 for index, entry in enumerate((prepEntry(hWidth, *s) for s in rows[:height]), start = 1):
70 lines[index].append(entry)
71 retLines = []
72 retLines.append(firstRowString.format(*tuple(lines[0])))
73 for line in lines[1:]:
74 retLines.append(rowString.format(*tuple(line)))
75 return '\n'.join(retLines)
187def print_table(values, fieldnames):
188 values.sort()
189 x = PrettyTable()
190 x.field_names = fieldnames
191 for field in fieldnames:
192 x.align[field] = "l"
193
194 for value in values:
195 x.add_row(value)
196
197 print(x)
131def print_table(column_headings, row_entries, alignment, file=sys.stdout):
132 """
133 Given the column headings and the row_entries, print a table.
134 Align according to alignment specification and always pad with 2 spaces.
135
136 :param column_headings: the column headings
137 :type column_headings: list of str
138 :param row_entries: a list of the row entries
139 :type row_entries: list of list of str
140 :param alignment: the alignment indicator for each key, '<', '>', '^', '='
141 :type alignment: list of str
142 :param file: file to print too
143 :type file: writeable stream
144
145 Precondition: len(column_headings) == len(alignment) == len of each entry
146 in row_entries.
147
148 Precondition: all(wcswidth(h) != -1 for h in column_headings)
149 all(wcswidth(i) != -1 for row in rows for item in row)
150 (i.e., no items to be printed contain unprintable characters)
151 """
152 column_widths = [0] * len(column_headings)
153 cell_widths = []
154
155 # Column header isn't different than any other row, insert into rows.
156 row_entries.insert(0, column_headings)
157
158 for row_index, row in enumerate(row_entries):
159 cell_widths.append([])
160 for column_index, cell in enumerate(row):
161 cell_width = maybe_wcswidth(cell)
162 cell_widths[row_index].append(cell_width)
163 column_widths[column_index] = max(column_widths[column_index], cell_width)
164
165 for row, row_widths in zip(row_entries, cell_widths):
166 _print_row(file, row, row_widths, column_widths, alignment)
167 print(file=file)
86def __prints(table):
87 node = tables[table]
88 __do_print(node)
252def print_table(table_data):
253 table = AsciiTable(table_data)
254 table.inner_row_border = True
255 if table_data[:1] in ([['TITLE', 'IMDB RATING']],
256 [['TITLE', 'TOMATO RATING']]):
257 table.justify_columns[1] = 'center'
258 print("\n")
259 print(table.table)
137def pprint_table(out, table):
138 """Prints out a table of data, padded for alignment
139 @param out: Output stream (file-like object)
140 @param table: The table to print. A list of lists.
141 Each row must have the same number of columns. """
142 col_paddings = []
143
144
145 for i in range(len(table[0])):
146 col_paddings.append(get_max_width(table, i))
147
148 for row in table:
149 # left col
150 print >> out, row[0].ljust(col_paddings[0] + 1),
151 # rest of the cols
152 for i in range(1, len(row)):
153 col = format_num(row[i]).rjust(col_paddings[i] + 2)
154 print >> out, col,
155 print >> out

Related snippets