简体   繁体   English

在 python 中并排打印列

[英]Print columns side by side in python

When applying this code: (for example)应用此代码时:(例如)

x = ["Apple\napple", "Orange\norange", "Banana\nbanana"]
for i in x:
    print(i, end = " ")

The result is:结果是:

Apple
apple Orange
orange Banana
banana

But I need a way to make it like this:但我需要一种方法来使它像这样:

Apple Orange Banana
apple orange banana
  • You need to split element of x to y first您需要先将 x 的元素拆分为 y
  • Then print indiced index of element in y然后打印 y 中元素的索引索引

Try this code below试试下面的代码

x = ["Apple\napple", "Orange\norange", "Banana\nbanana"]
y = [x[i].split() for i in range(len(x))]
print(f'{y[0][0]} {y[1][0]} {y[2][0]}\n{y[0][1]} {y[1][1]} {y[2][1]}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM