如何实现具有特定元素子集的 table

How to realize a table with a specific elements subset

我必须通过如下列表勾勒出具有十个元素的周期性 table:

pt = [(1,"Hydrogen","H",1), (2,"Helium","He",4), ...].

如下,将table中对应列的宽度和要求对齐

 (3 right + 20 left + 6 center + 10 right).

我应该得到的最终结果是这样的。

我已尝试通过 package tabulate 创建,遵循此过程

pt = [(1,"Hydrogen","H",1.008), (2,"Helium","He",4.0026), (3, "Lithium", "Li", 6.94), (4, "Beryllium", "Be", 9.0122),
      (5, "Boron", "B", 10.81), (6, "Carbon", "C", 12.011), (7, "Nitrogen", "N", 14.007), (8, "Oxygen", "O", 15.999),
      (9, "Fluorine", "F", 18.998), (10, "Neon", "Ne", 20.180)]
head = ["No.", "Name", "Symbol", "Weight"]
print(tabulate(pt, headers=head, tablefmt="grid"))

效果不错

但由于我必须尊重宽度尺寸,如上所述,我不知道如何将它们包含在代码中。有人建议我手动创建前三行,但应在 'for' loop (for an item in pt:) 中创建包含化学元素的行。对字符串 (rjust, center, ljust).

使用方法

有人知道怎么做吗?

由于您使用制表,因此有一种解决方案可以通过以下方式修改您的代码:

  1. 为数组末尾的每一列添加一个额外的行,其中包含指定大小的字符
  2. 调用 tabulate 包括使用 coalign
  3. 的对齐规范
  4. 删除多余的行

由于输出是一个字符串,列的宽度与任何度量无关,例如以厘米为单位,因此您需要定义适合您需要的长度比例,即每个所需宽度单位的字符数。

请注意,从计算的宽度中删除了 2 个字符,以说明左右各 1 个字符的内列边距。

from tabulate import tabulate

pt = [(1,"Hydrogen","H",1.008), (2,"Helium","He",4.0026), (3, "Lithium", "Li", 6.94), (4, "Beryllium", "Be", 9.0122),
      (5, "Boron", "B", 10.81), (6, "Carbon", "C", 12.011), (7, "Nitrogen", "N", 14.007), (8, "Oxygen", "O", 15.999),
      (9, "Fluorine", "F", 18.998), (10, "Neon", "Ne", 20.180)]
head = ["No.", "Name", "Symbol", "Weight"]

lengthScale = 2
widths = [3, 20, 6, 10]
pt.append(["-"*(width*lengthScale-2) for width in widths])

tab = tabulate(pt, headers=head, tablefmt="grid", colalign=("right","left","center","right"))

for line in range(2):
    tab = tab[:tab.rfind('\n')]

print(tab)

它产生:

+-------+----------------------------------------+------------+--------------------+
|   No. | Name                                   |   Symbol   |             Weight |
+=======+========================================+============+====================+
|     1 | Hydrogen                               |     H      |              1.008 |
+-------+----------------------------------------+------------+--------------------+
|     2 | Helium                                 |     He     |             4.0026 |
+-------+----------------------------------------+------------+--------------------+
|     3 | Lithium                                |     Li     |               6.94 |
+-------+----------------------------------------+------------+--------------------+
|     4 | Beryllium                              |     Be     |             9.0122 |
+-------+----------------------------------------+------------+--------------------+
|     5 | Boron                                  |     B      |              10.81 |
+-------+----------------------------------------+------------+--------------------+
|     6 | Carbon                                 |     C      |             12.011 |
+-------+----------------------------------------+------------+--------------------+
|     7 | Nitrogen                               |     N      |             14.007 |
+-------+----------------------------------------+------------+--------------------+
|     8 | Oxygen                                 |     O      |             15.999 |
+-------+----------------------------------------+------------+--------------------+
|     9 | Fluorine                               |     F      |             18.998 |
+-------+----------------------------------------+------------+--------------------+
|    10 | Neon                                   |     Ne     |              20.18 |
+-------+----------------------------------------+------------+--------------------+