Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中从元组列表创建表_Python_List_Python 3.x_Formatting_Iteration - Fatal编程技术网

在Python3中从元组列表创建表

在Python3中从元组列表创建表,python,list,python-3.x,formatting,iteration,Python,List,Python 3.x,Formatting,Iteration,我有家庭作业,我必须拿出一个包含元组的列表,然后打印出一个表格。例如,列表可能如下所示: data = [('Item1', a1, b1, c1, d1, e1, f1), ('Item2', a2, b2, c2, d2, e2, f2), ('Item3', a3, b3, c3, d3, e3, f3)] 我必须打印出以下内容: Item1 Item2 Item3 DataA: a1 a2 a

我有家庭作业,我必须拿出一个包含元组的列表,然后打印出一个表格。例如,列表可能如下所示:

data = [('Item1', a1, b1, c1, d1, e1, f1),
        ('Item2', a2, b2, c2, d2, e2, f2),
        ('Item3', a3, b3, c3, d3, e3, f3)]
我必须打印出以下内容:

            Item1   Item2   Item3
DataA:      a1      a2      a3
DataB:      b1      b2      b3
DataC:      c1      c2      c3
DataD:      d1      d2      d3
DataE:      e1      e2      e3
DataF:      f1      f2      f3
我已草签了一份清单:

data_headings = ['','DataA:','DataB','DataC:','DataD:','DataE':,'DataF:']
我的老师还让我们选择使用他创建的函数:

display_with_padding(str):
     print("{0: <15}".format(s), end = '')
display\u with_padding(str):

print(“{0:诀窍是以某种顺序迭代表中的每个元素。是先行还是先列

因为要逐行打印,所以必须先遍历行,然后在每列中查找相应的值。请注意,
data
是列的列表,而第二个列表为每行提供一个标签。在下面的演示中,我已将其重命名为
row\u labels

def display_with_padding(s):
  print("{0: <15}".format(s), end = '')

data = [('Item1', 'a1', 'b1', 'c1', 'd1', 'e1', 'f1'),
        ('Item2', 'a2', 'b2', 'c2', 'd2', 'e2', 'f2'),
        ('Item3', 'a3', 'b3', 'c3', 'd3', 'e3', 'f3')]

row_labels = ['', 'DataA:', 'DataB:', 'DataC:', 'DataD:', 'DataE:', 'DataF:']

for row_index, label in enumerate(row_labels):
  display_with_padding(label)
  for column in data:
    display_with_padding(column[row_index])
  print()
def显示带填充:
打印({0:
def display_,带填充):

打印({0:我回答你的问题了吗?
def display_with_padding(s):
     print("{0: <15}".format(s), end='')

def print_row(iterable):
    [display_with_padding(x) for x in iterable]
    print()

def main():

    data = [
        ('Item1', 'a1', 'b1', 'c1', 'd1', 'e1', 'f1'),
        ('Item2', 'a2', 'b2', 'c2', 'd2', 'e2', 'f2'),
        ('Item3', 'a3', 'b3', 'c3', 'd3', 'e3', 'f3')
    ]

    col_headers = [''] + [x[0] for x in data]  # Build headers
    print_row(col_headers)


    labels = ['DataA:','DataB:','DataC:','DataD:','DataE:','DataF:']

    # Build each row
    rows = []
    for row_num, label in enumerate(labels, start=1):
        content = [label]
        for col in data:
            content.append(col[row_num])
        rows.append(content)

    for row in rows:
        print_row(row)


if __name__ == '__main__':
    main()