Printing all the values from multiple lists at the same time

后端 未结 7 1911
遥遥无期
遥遥无期 2020-12-19 05:02

Suppose I have 3 lists such as these

l1 = [1,2,3]
l2 = [4,5,6]
l3 = [7,8,9]

how do I get to print out everything from these lists at the s

7条回答
  •  情书的邮戳
    2020-12-19 05:49

    It depends on what you want to achieve,

    >>> #Given
    >>> l1,l2,l3 = [1,2,3],[4,5,6],[7,8,9]
    >>> #To print row wise
    >>> import itertools
    >>> for f in itertools.chain(l1,l2,l3):
        print(f,end=" ")
    
    
    1 2 3 4 5 6 7 8 9 
    >>> #To print column wise
    >>> for f in itertools.izip(l1,l2,l3):
        print(*f,end=" ")
    
    
    1 4 7 2 5 8 3 6 9 
    >>> 
    

    or the following implementation which will work in Python 2.7

    >>> for f in itertools.chain(*itertools.izip(l1,l2,l3)):
        print f,
    
    
    1 4 7 2 5 8 3 6 9 
    >>> 
    

提交回复
热议问题