【问题标题】:Created table from list not showing all elements从列表创建的表未显示所有元素
【发布时间】:2013-07-18 21:18:28
【问题描述】:

我有一个函数接受列表作为参数,该参数已经从另一个函数创建。该列表以列行格式打印。此函数应假定列表中的第一项包括列标题,并且列表的其余部分包括值。 showList() 函数应该能够整齐地显示由空格或制表符分隔的 2 到 5 列的列表。在大多数情况下,它运行得相当好,但当它有多个国家要列出时,它只列出一个。这是它的外观示例:

Country  Gold   Silver Bronze
(this space is actually a sequence of equal signs to represent a heading line)
United States 765    555    780
Great Britain 600  200      950
def showList(returned_List):
    header = '     '
    line = ''
    entries = ''
    for i in returned_List[0]:
        header += i + (' ')*5
    for k in range(len(header)):
        line += '='
    for j in returned_List[1]:
        entries += j +(' ')*5
    print(header, '\n', line, '\n', entries)
    return(returned_List)

【问题讨论】:

标签: python list file printing


【解决方案1】:

您需要做的就是遍历returned_list 中的行,而不是硬编码returned_list[0]

def showList(returned_List):
    header = '     '
    line = ''
    entries = ''
    for row in returned_list:
        for i in row:
            header += i + (' ')*5
        for k in range(len(header)):
            line += '='
        for j in returned_List[1]:
            entries += j +(' ')*5
        print(header, '\n', line, '\n', entries)
        return(returned_List)

从您的 cmets 中,我可以看出您在寻找什么。这是我不久前编写的脚本改编版,对您有帮助:

def tabularize(inData, outfilepath):
    """ Return nothing
        Write into the file in outfilepath, the contents of inData, expressed in tabular form.
        The tabular form is similar to the way in which SQL tables are displayed.
    """

    widths = [max([len(row) for row in rows])+2 for rows in izip_longest(*inData, fillvalue="")]

    with open(outfilepath, 'w') as outfile:
        outfile.write("+")
        for width in widths:
            outfile.write('-'*width + "+")
        outfile.write('\n')
        for line in lines:
            outfile.write("|")
            for col,width in izip_longest(line,widths, fillvalue=""):
                outfile.write("%s%s%s|" %(' '*((width-len(col))/2), col, ' '*((width+1-len(col))/2)))
            outfile.write('\n+')
            for width in widths:
                outfile.write('-'*width + "+")


outfile.write('\n')

if __name__ == "__main__":
    print 'starting'

    tabularize(infilepath, outfilepath, '...', False)

    print 'done'

希望对你有帮助

【讨论】:

  • 国家代码 金银铜 美国 美国 1063 853 737 =============================== ================================================== ================================================== ==================================== 美国 美国 1063 853 737 美国 美国 1063 853 737 这有点有问题的。这是输出:
  • @JacobHahn:cmets 中缺少格式是有问题的,但我想我理解这个问题。但是,在调用此函数之前,我不确定是否可以在没有看到returned_list 的值的情况下修复很多问题
  • 这里是返回列表的示例。无论子列表中的元素数量如何,列表中的第一个元素始终是标题 [['Code', 'Country'], ['TKM', 'Turkmenistan'], ['PAK', 'Pakistan'], ['KAZ', '哈萨克斯坦'], ['CRC', '哥斯达黎加'], ['AFG', '阿富汗'], ['TJK', '塔吉克斯坦'], ['UZB', '乌兹别克斯坦'] , ['KGZ', '吉尔吉斯斯坦']]
【解决方案2】:

for i in returned_list[0]: 只查看第一个条目。

你可能想要:

对于返回列表中的 i:

【讨论】:

  • 没有。 returned_list 中的每个元素本身就是一个列表。这些子列表包含 OP 要打印的列值。所以你想要for sublist in returned_list: for i in sublist:my answer
猜你喜欢
  • 1970-01-01
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 2012-10-18
  • 1970-01-01
  • 2010-10-20
相关资源
最近更新 更多