使用 python Beautiful Table 模块打印多维数组

Printing out multidimensional arrays using python Beautiful Table module

我正在尝试使用 Python2.7.

中的 Beautiful Table 模块以表格格式打印多维数组

我试过以下代码:

from beautifultable import BeautifulTable
tableA = BeautifulTable()
tableA.column_headers = ["possible Inflow", "prob", "possibleinlow * prob", "d12p"]
data = [['a', 1, 2, 2], [4.0, 3.0, 3.0, 2.0], [2.0, 4.0, 1.0, 4.0], [  8.,  12.,   3.,   8.], [ 1458.,  3136.,   784.,  3364.]]
tableA.column_headers = ["inputs", "possible Inflow", "prob", "possibleinlow * prob", "d12p"]
for i in range(len(data)):
    tableA.append_row([item[i] for item in data])
print(tableA)

但它打印出以下错误:

Traceback (most recent call last):File "test.py", line 16, in <module>

"raise ValueError("'Expected iterable of length {}, got {}".format(self._column_count, len(row)))

ValueError: '预期长度为 4 的迭代器,得到了 5

@Imm,假设你是第一个

tableA.column_headers = 

是您想要的(4 项),并且您有 5 个数组,每组 4 个项作为行添加到 table,那么您可能需要更简单的程序:

from beautifultable import BeautifulTable
tableA = BeautifulTable()
tableA.column_headers = ["possible Inflow", "prob", "possibleinlow * prob", "d12p"]
data = [['a', 1, 2, 2], [4.0, 3.0, 3.0, 2.0], [2.0, 4.0, 1.0, 4.0],\
 [8., 12., 3., 8.], [ 1458., 3136., 784., 3364.]]
for i in range(len(data)):
     tableA.append_row(data[i])
print(tableA)

... 产生:

+-----------------+------+----------------------------------------------+------+
| possible Inflow | prob |             possibleinlow * prob             | d12p |
+-----------------+------+----------------------------------------------+------+
|        a        |  1   |                      2                       |  2   |
+-----------------+------+----------------------------------------------+------+
|        4        |  3   |                      3                       |  2   |
+-----------------+------+----------------------------------------------+------+
|        2        |  4   |                      1                       |  4   |
+-----------------+------+----------------------------------------------+------+
|        8        |  12  |                      3                       |  8   |
+-----------------+------+----------------------------------------------+------+
|      1458       | 3136 |                     784                      | 3364 |
+-----------------+------+----------------------------------------------+------+

如果您想使用第二个

tabelA.column_headers = 

行,而你的数据是一组列信息,那么只需使用 BeautifulTable 的一种列方法即可。

from beautifultable import BeautifulTable
tableA = BeautifulTable()
columnHeaders = ["inputs", "possible Inflow", "prob", "possibleinlow * prob", "d12p"]
data = [['a', 1, 2, 2], [4.0, 3.0, 3.0, 2.0], [2.0, 4.0, 1.0, 4.0],\
 [8., 12., 3., 8.], [1458., 3136., 784., 3364.]]
for i in range(len(columnHeaders)):
    tableA.insert_column(i, columnHeaders[i], data[i])
print(tableA)

... 产生:

+--------+------------------------+------+------------------------------+------+
| inputs |    possible Inflow     | prob |     possibleinlow * prob     | d12p |
+--------+------------------------+------+------------------------------+------+
|   a    |           4            |  2   |              8               | 1458 |
+--------+------------------------+------+------------------------------+------+
|   1    |           3            |  4   |              12              | 3136 |
+--------+------------------------+------+------------------------------+------+
|   2    |           3            |  1   |              3               | 784  |
+--------+------------------------+------+------------------------------+------+
|   2    |           2            |  4   |              8               | 3364 |
+--------+------------------------+------+------------------------------+------+

添加一些打印行可以帮助理解事物。 干杯!