Python 如何将多个列表显示为一个表,每个列表具有不同的索引长度

Python 如何将多个列表显示为一个表,每个列表具有不同的索引长度,python,python-3.x,Python,Python 3.x,如果我有3个列表,每个列表都有不同的长度,而不使用外部库,那么如何显示一个表。(PYTHON 3) 有以下数据输入: list_1 = ["Ae Ae", "Bee Bee", "Ce Ce", "Dee Dee"] list_2 = ["Af Af", "Bf Bf"] list_3 = [] 以下是我的建议: e NAME | f NAME | g NAME ============= | ============= | ===

如果我有3个列表,每个列表都有不同的长度,而不使用外部库,那么如何显示一个表。(PYTHON 3)

有以下数据输入:

list_1 = ["Ae Ae", "Bee Bee", "Ce Ce", "Dee Dee"]
list_2 = ["Af Af", "Bf Bf"]
list_3 = []
以下是我的建议:

    e NAME      |     f NAME       |      g NAME
=============   |  =============   |   =============
Ae Ae           |  Af Af           |
Bee Bee         |  Bf Bf           |
Ce Ce           |                  |
Dee Dee         |                  |
谢谢。
(外部库是指Python3中没有默认的库。(例如:import random就可以了)。

我会让您对边距感兴趣

from itertools import zip_longest

list_1 = ["Ae Ae", "Bee Bee", "Ce Ce", "Dee Dee"] 
list_2 = ["Af Af", "Bf Bf"] 
list_3 = [] 

for strs in zip_longest(list_1, list_2, list_3): 
    for s in strs: 
        if(s is not None): 
            print(s, end="") 
        print("\t|", end="") 
    print("")

由于某些原因,选项卡在这里没有对齐,但在我的python和ipython shell中它们看起来不错。

我使用与@curlycharcoal相同的
zip\u longest
对行中的元素进行分组,但后来我使用了

  • 列出要替换的理解
    None
  • 字符串格式设置为所有项目设置相同的宽度
  • “|”。join()
    创建包含整行的字符串

结果

      e NAME      |     f NAME       |      g NAME
  =============   |  =============   |   =============
  Ae Ae           |  Af Af           |                  
  Bee Bee         |  Bf Bf           |                  
  Ce Ce           |                  |                  
  Dee Dee         |                  |                  

编辑:

类似的方法我可以创建标题。但这次我使用字符串格式中的
^
来居中文本

我使用
“=”*14
创建
===================

header = ['e NAME', 'f NAME', 'g NAME']

row = ["  {:^14}  ".format(item)
              for item in header]

print("|".join(row))

row = ["  " + "="*14 + "  "
          for item in header]

print("|".join(row))
稍后我可以使用变量
width
而不是
14
,它将使用它来代替内部
{}

"  {:^{}}  ".format(item, width)
通过这种方式,我可以轻松地更改
宽度
(最终我可以使用
max()
len()
在所有表或每列中查找最长的单词,并对每列使用不同的宽度,但我不会在这里尝试这样做)



有关上字符串格式的详细信息


顺便说一句:如果您不希望在第一列前面有两个空格,请在
“|”中使用空格,而不要在
“{:^14}”中使用空格。

结果(左侧没有两个空格)

欢迎使用。请注意,这不是一项代码编写服务。我们可以帮助解决特定的技术问题,而不是开放式的代码或建议请求。请通过您的问题说明您迄今为止尝试了什么,以及您需要帮助的具体问题。有关如何帮助我们帮助您的详细信息,请参阅页面。
"  {:^{}}  ".format(item, width)
from itertools import zip_longest

list_1 = ["Ae Ae", "Bee Bee", "Ce Ce", "Dee Dee"] 
list_2 = ["Af Af", "Bf Bf"] 
list_3 = [] 

header = ['e NAME', 'f NAME', 'g NAME']

#width = 14
width = 20

row = ["  {:^{}}  ".format(item, width)
              for item in header]

print("|".join(row))

row = ["  " + "="*width + "  "
          for item in header]

print("|".join(row))


for row in zip_longest(list_1, list_2, list_3):

    # put empty string instead of `None`
    row = ["" if item is None else item
              for item in row]

    # format every item to the same length
    row = ["  {:{}}  ".format(item, width)
              for item in row]

    # join all items in row using `|` and display row
    print("|".join(row))
from itertools import zip_longest

list_1 = ["Ae Ae", "Bee Bee", "Ce Ce", "Dee Dee"] 
list_2 = ["Af Af", "Bf Bf"] 
list_3 = [] 

print('''    e NAME      |     f NAME       |      g NAME
=============   |  =============   |   =============''')

for row in zip_longest(list_1, list_2, list_3):

    # put empty string instead of `None`
    row = ["" if item is None else item
              for item in row]

    # format every item to the same width
    row = ["{:14}".format(item)
              for item in row]

    # join all items in row using `|` and display row
    print("  |  ".join(row))
    e NAME      |     f NAME       |      g NAME
=============   |  =============   |   =============
Ae Ae           |  Af Af           |                
Bee Bee         |  Bf Bf           |                
Ce Ce           |                  |                
Dee Dee         |                  |