Print list in table format in python

后端 未结 5 1496
春和景丽
春和景丽 2020-12-31 18:27

I am trying to print several lists (equal length) as columns of an table.

I am reading data from a .txt file, and at the end of the code, I have 5 lists, which I wou

相关标签:
5条回答
  • 2020-12-31 18:44

    Assming that you have a lists of lists:

    for L in list_of_lists:
        print " ".join(L)
    

    The str.join(iterable) function, joins the components of an iterable by the string given.

    Therefore, " ".join([1, 2, 3]) becomes "1 2 3".

    In case I might have misunderstood the question and each list is supposed to be a column:

    for T in zip(list1, list2, list3, list4, list5):
        print " ".join(T)
    

    zip() merges the given lists to one list of tuples:

    >>> zip([1,2,3], [4,5,6], [7,8,9])
    [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
    

    Cheers!

    0 讨论(0)
  • 2020-12-31 18:44

    Try out my library

    NOTE: This answer is already posted on this question, and this question.

    I just made a library for this that I think could really help. It is extremely simple, that's why I think you should use it. It is called TableIT.

    Basic Use

    To use it, first follow the download instructions on the GitHub Page.

    Then import it:

    import TableIt
    

    Then make a list of lists where each inner list is a row:

    table = [
        [4, 3, "Hi"],
        [2, 1, 808890312093],
        [5, "Hi", "Bye"]
    ]
    

    Then all you have to do is print it:

    TableIt.printTable(table)
    

    This is the output you get:

    +--------------------------------------------+
    | 4            | 3            | Hi           |
    | 2            | 1            | 808890312093 |
    | 5            | Hi           | Bye          |
    +--------------------------------------------+
    

    Field Names

    You can use field names if you want to (if you aren't using field names you don't have to say useFieldNames=False because it is set to that by default):

    
    TableIt.printTable(table, useFieldNames=True)
    

    From that you will get:

    +--------------------------------------------+
    | 4            | 3            | Hi           |
    +--------------+--------------+--------------+
    | 2            | 1            | 808890312093 |
    | 5            | Hi           | Bye          |
    +--------------------------------------------+
    

    There are other uses to, for example you could do this:

    import TableIt
    
    myList = [
        ["Name", "Email"],
        ["Richard", "richard@fakeemail.com"],
        ["Tasha", "tash@fakeemail.com"]
    ]
    
    TableIt.print(myList, useFieldNames=True)
    

    From that:

    +-----------------------------------------------+
    | Name                  | Email                 |
    +-----------------------+-----------------------+
    | Richard               | richard@fakeemail.com |
    | Tasha                 | tash@fakeemail.com    |
    +-----------------------------------------------+
    

    Or you could do:

    import TableIt
    
    myList = [
        ["", "a", "b"],
        ["x", "a + x", "a + b"],
        ["z", "a + z", "z + b"]
    ]
    
    TableIt.printTable(myList, useFieldNames=True)
    

    And from that you get:

    +-----------------------+
    |       | a     | b     |
    +-------+-------+-------+
    | x     | a + x | a + b |
    | z     | a + z | z + b |
    +-----------------------+
    

    Colors

    You can also use colors.

    You use colors by using the color option (by default it is set to None) and specifying RGB values.

    Using the example from above:

    import TableIt
    
    myList = [
        ["", "a", "b"],
        ["x", "a + x", "a + b"],
        ["z", "a + z", "z + b"]
    ]
    
    TableIt.printTable(myList, useFieldNames=True, color=(26, 156, 171))
    

    Then you will get:

    Please note that printing colors might not work for you but it does works the exact same as the other libraries that print colored text. I have tested and every single color works. The blue is not messed up either as it would if using the default 34m ANSI escape sequence (if you don't know what that is it doesn't matter). Anyway, it all comes from the fact that every color is RGB value rather than a system default.

    More Info

    For more info check the GitHub Page

    0 讨论(0)
  • 2020-12-31 18:46
    for nested_list in big_container_list
        print '\t'.join(nested_list)
    

    with \t being the tabulation character

    quick example:

    In [1]: a = [['1','2'],['3','4']]
    In [5]: for nested_list in a:
    ...:     print '\t'.join(nested_list)
    ...: 
    1       2
    3       4
    
    0 讨论(0)
  • 2020-12-31 18:54

    You can use my package beautifultable . It supports adding data by rows or columns or even mixing both the approaches. You can insert, remove, update any row or column.

    Usage

    >>> from beautifultable import BeautifulTable
    >>> table = BeautifulTable()
    >>> table.column_headers = ["name", "rank", "gender"]
    >>> table.append_row(["Jacob", 1, "boy"])
    >>> table.append_row(["Isabella", 1, "girl"])
    >>> table.append_row(["Ethan", 2, "boy"])
    >>> table.append_row(["Sophia", 2, "girl"])
    >>> table.append_row(["Michael", 3, "boy"])
    >>> print(table)
    +----------+------+--------+
    |   name   | rank | gender |
    +----------+------+--------+
    |  Jacob   |  1   |  boy   |
    +----------+------+--------+
    | Isabella |  1   |  girl  |
    +----------+------+--------+
    |  Ethan   |  2   |  boy   |
    +----------+------+--------+
    |  Sophia  |  2   |  girl  |
    +----------+------+--------+
    | Michael  |  3   |  boy   |
    +----------+------+--------+
    

    Have fun

    0 讨论(0)
  • 2020-12-31 19:08

    I'll show you a 3-list analog:

    >>> l1 = ['a', 'b', 'c']
    >>> l2 = ['1', '2', '3']
    >>> l3 = ['x', 'y', 'z']
    >>> for row in zip(l1, l2, l3):
    ...     print ' '.join(row)
    
    a 1 x
    b 2 y
    c 3 z
    
    0 讨论(0)
提交回复
热议问题