由于您对Python还不熟悉,根据Zen of Python的简单优于复杂,这里有一个只使用最简单Python结构的解决方案:
from __future__ import print_function
str_ = ["abcd", "12", "x"]
max_len = max(len(i) for i in str_)
out = ""
for i in range(max_len):
for j in str_:
try:
out += j[i]
except IndexError:
out += " "
print(out)
izip_longest
来自itertools
是你的朋友。它使用最长的iterable(此处为字符串),并用可设置为所需空格的填充值替换缺少的字符:结果:
用
chain
代替map
和第二个join
的变体。你知道吗应用于数组
a
的最后一个方法的中间步骤:结果:
由于您对Python还不熟悉,根据Zen of Python的简单优于复杂,这里有一个只使用最简单Python结构的解决方案:
作为SO的新手,我建议您阅读https://stackoverflow.com/help/on-topichttps://stackoverflow.com/questions/how-to-ask和https://stackoverflow.com/help/mcve。你知道吗
相关问题 更多 >
编程相关推荐