到目前为止,这是我的代码:

def main():
    places=["Hawaii","Ohio","Tokyo","Korea"]
    print(places,"\n")
    for i in range(0,len(places[0])):
        print(places[0][i])
    for i in range(0,len(places[1])):
        print(places[1][i])
    for i in range(0,len(places[2])):
            print(places[2][i])
    for i in range(0,len(places[3])):
            print(places[3][i])

main()


我正在尝试并排垂直打印4个单词

最佳答案

向@Ryan喊出建议

from itertools import zip_longest

def main():
    for a, b, c, d in zip_longest(*["Hawaii", "Ohio", "Tokyo", "Korea"], fillvalue=" "):
        print(a, b, c, d)

main()


输出:

H O T K
a h o o
w i k r
a o y e
i   o a
i


使用嵌套的for循环进行编辑:

def main2():
    places = ["Hawaii", "Ohio", "Tokyo", "Korea"]
    for i in range(6):
        for j in range(4):
            try:
                print(places[j][i], end=' ')
            except:
                print(' ', end=' ')
        print()

关于python - 如何垂直并排打印列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47403273/

10-12 22:50