Python - write list list of lists in columns

前端 未结 2 1146
予麋鹿
予麋鹿 2020-12-10 17:11

I have a list of lists with a various number of elements (int). I want to print/write it, but in columns rather than in rows.

Example:

l = [[1,2,3],[         


        
2条回答
  •  囚心锁ツ
    2020-12-10 17:30

    This:

    import itertools
    
    l = [[1,2,3],[4,5],[6,7,8,9],[0]]
    
    for t in itertools.izip_longest(*l):
        print "".join("%3d" % x if x is not None else "  ." for x in t)
    

    produces:

      1  4  6  0
      2  5  7  .
      3  .  8  .
      .  .  9  .
    

提交回复
热议问题