Python 如何垂直并排打印列表?
这是我目前的代码:Python 如何垂直并排打印列表?,python,python-3.x,list,loops,python-2.x,Python,Python 3.x,List,Loops,Python 2.x,这是我目前的代码: 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(pla
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个单词垂直并排打印出来,你需要这个吗
places=["Hawaii","Ohio","Tokyo","Korea"]
vertical_list = [i for place in places for i in list(place)]
for letter in vertical_list:
print(letter)
输出:
H
a
w
a
i
i
O
h
i
o
T
o
k
y
o
K
o
r
e
a
H O T K
a h o o
w i k r
a o y e
i o a
i
你需要这个吗
places=["Hawaii","Ohio","Tokyo","Korea"]
vertical_list = [i for place in places for i in list(place)]
for letter in vertical_list:
print(letter)
输出:
H
a
w
a
i
i
O
h
i
o
T
o
k
y
o
K
o
r
e
a
H O T K
a h o o
w i k r
a o y e
i o a
i
向@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
a
w
a
i
i
O
h
i
o
T
o
k
y
o
K
o
r
e
a
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()
向@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
a
w
a
i
i
O
h
i
o
T
o
k
y
o
K
o
r
e
a
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()
这里有一个通用的解决方案,不管您有多少项。可以进行一些优化,此代码旨在实现最大的清晰度
places=["Hawaii","Ohio","Tokyo","Korea"]
#Find longest word
max_len = max([len(place) for place in places])
# Loop over words and pad them with spaces
for i, place in enumerate(places):
if len(place) < max_len:
places[i] = place.ljust(max_len)
# Print the words one letter at a time.
for i in range(max_len):
print(" ".join([place[i] for place in places]))
places=[“夏威夷”、“俄亥俄”、“东京”、“韩国”]
#查找最长单词
max_len=max([len(place)表示place in places])
#在单词上循环并用空格填充
对于i,在枚举中的位置(位置):
如果长度(位置)<最大长度:
places[i]=place.ljust(max_len)
#一次打印一个字母。
对于范围内的i(最大值):
打印(“.join”([place[i]表示place in places]))
无论您有多少物品,这里都有一个通用的解决方案。可以进行一些优化,此代码旨在实现最大的清晰度
places=["Hawaii","Ohio","Tokyo","Korea"]
#Find longest word
max_len = max([len(place) for place in places])
# Loop over words and pad them with spaces
for i, place in enumerate(places):
if len(place) < max_len:
places[i] = place.ljust(max_len)
# Print the words one letter at a time.
for i in range(max_len):
print(" ".join([place[i] for place in places]))
places=[“夏威夷”、“俄亥俄”、“东京”、“韩国”]
#查找最长单词
max_len=max([len(place)表示place in places])
#在单词上循环并用空格填充
对于i,在枚举中的位置(位置):
如果长度(位置)<最大长度:
places[i]=place.ljust(max_len)
#一次打印一个字母。
对于范围内的i(最大值):
打印(“.join”([place[i]表示place in places]))
您可能最终会想用您可能最终会想用