[英]How to iterate through string elements in a list side-by-side?
I have a list:我有一个清单:
cl = ["Food", "Clothing", "Automation"]
I want to print each character in each string of this list side-by-side:我想并排打印此列表的每个字符串中的每个字符:
F C A
o l u
o o t
d t o
h
i
n
g
I have managed to do this so far:到目前为止,我已经设法做到了:
cl = ["Food", "Clothing", "Automation"]
for i in cl:
for j in i:
print(j)
Output:输出:
F
o
o
d
C
l
o
t
h
i
n
g
A
u
t
o
m
a
t
i
o
n
How can I achieve my desired outcome?我怎样才能达到我想要的结果?
You can use itertools.zip_longest
:您可以使用
itertools.zip_longest
:
from itertools import zip_longest
cl = ['Food', 'Clothing', 'Automation']
for letters in zip_longest(*cl, fillvalue = ' '):
print(*letters, sep = ' ')
Output:输出:
F C A
o l u
o o t
d t o
h m
i a
n t
g i
o
n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.