从列表创建的表未显示所有元素 [英] Created table from list not showing all elements

查看:76
本文介绍了从列表创建的表未显示所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,该函数接受list作为已经从另一个函数创建的参数.该列表以列行格式打印.此函数应假定列表中的第一项包括列标题,而列表的其余部分包括值. showList()函数应该能够整齐地显示由空格或制表符分隔的2至5列的列表.在大多数情况下,它运作良好,但是要列出一个以上的国家时,只会列出一个国家.这是一个看起来像的例子:

I have a function that accept list as a parameter that was already created from another function. The list is printed in column row format. This function should assume that the first item in the list includes column titles and that the rest of the list includes values. The showList() function should be able to neatly display a list of 2 to 5 columns separated by spaces or tabs. in most cases it works fairly well but when it has more than one country to list it only lists one. Here's an example of what it looks like:

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)

推荐答案

所有您需要做的就是循环遍历returned_list中的行,而不是对returned_list[0]进行硬编码:

All you need to do is loop over the rows in returned_list, instead of hard-coding 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)

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

From your comments, I kinda see what you are looking for. Here's an adaptation of script I wrote a while back that'll help you:

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'

希望这会有所帮助

这篇关于从列表创建的表未显示所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆