我有三个不同大小的数组要打印到一个选项卡中

2024-05-13 23:24:48 发布

您现在位置:Python中文网/ 问答频道 /正文

a = [1, 2, 3]    
b = [a, b, c, d]    
c = ["why", "cant", "i", "make", "this", "work"]

期望输出:

a     b     c  
1     a     why  
2     b     cant  
3     c     I  
      d     make  
            this  
            work  

Tags: makethisworkwhycant
1条回答
网友
1楼 · 发布于 2024-05-13 23:24:48

您可以引用此代码

a = [1, 2, 3]
b = ['a', 'b', 'c', 'd']
c = ["why", "cant", "i", "make", "this", "work"]

def get_safenode(l, i):
    if i<len(l):
        an=l[i]
    else:
        an=''
    return str(an)

maxlen = max(len(a), len(b), len(c))
print('%3s %3s %-5s' % ('a','b','c'))
for i in range(maxlen):
    an = get_safenode(a, i)
    bn = get_safenode(b, i)
    cn = get_safenode(c, i)
    print('%3s %3s %-5s'%(an, bn, cn))

输出是这样的

  a   b c    
  1   a why  
  2   b cant 
  3   c i    
      d make 
        this 
        work 

相关问题 更多 >