python - 在 python 中垂直打印行(不是列表)

标签 python line

<分区>

我想垂直打印字符串。我不确定当它不是列表形式时该怎么做...我提到了 izip 但我猜它只适用于列表???

Input
Output 
Variable
Constant

我最初有上面的列表,然后我做了

List = ['Input', 'Output', 'Variable', 'Constant')
for entry in List.strip().split(','):
    nospace_list = entry.lstrip()
    print nospace_list

上面代码的输出是我给出的,现在我想将其显示为

I O V C
n u a o
p t r n
u p i s
t u a t
  t b a
    l n
    e t

最佳答案

您似乎有兴趣为此编写您自己的算法,所以这里有一种(效率低得多)不使用 izip_longest() 的方法:

>>> words = ['Input', 'Output', 'Variable', 'Constant']
>>> longest = max(len(word) for word in words)
>>> for i in range(longest):
...     print ' '.join(word.ljust(longest)[i] for word in words)
... 
I O V C
n u a o
p t r n
u p i s
t u a t
  t b a
    l n
    e t

这是通过用空格填充每个单词到最长单词的长度,然后打印每个单词的每个字母来实现的。

关于python - 在 python 中垂直打印行(不是列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24665776/

相关文章:

python - 取得Pandas系列每周的第一天

python - 如何输出从列表中查找最大频率值的字典?

JavaScript Canvas ,获得平坦的线条

sed 删除行中除前 5 个字符以外的剩余字符

javascript - 将方形或圆形图像放入谷歌地图中的直线路径中

input - 如何避免在 Rust 中使用 stdin 换行

python - Beautiful Soup 找不到我想要的 HTML 部分

python - 为每个类类型自动生成唯一 ID

python - 如何放入一个表单来解析用户上传的文件,然后将其显示在新的 URL 上?

javascript - 有什么方法可以使用 Javascript 或 CSS 确定客户端浏览器中的哪一行中的哪些词?