Print list in table format in python

后端 未结 5 1497
春和景丽
春和景丽 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!

提交回复
热议问题