[英]Vertically flip ASCII art with Python
对于我正在处理的另一个代码,我需要垂直翻转ASCII图像:我要这样做:
*
***
*****
***
***
到这个:
***
***
*****
***
*
我现在所要做的就是将多行输入读取到一个数组中,但是我该如何使它最后打印第一个数组,然后首先打印底部数组。
text = ""
stopword = ""
while True:
line = input()
if line.strip() == stopword:
break
您可以将每行添加到行列表( list.append
),然后在打印之前反转该列表( list[::-1]
):
lines = []
stopword = ""
while True:
line = input()
if line.strip() == stopword:
break
lines.append(line) # Add to the list of lines
for line in lines[::-1]: # [::-1] inverts the list
print(line)
这是deque
的合理用例-您可以将.extendleft
与任何可迭代.extendleft
一起使用。
from collections import deque
stop_word = '' # an empty line causes a stop
lines_until_stop = iter(input, stopword)
d = deque()
d.extendleft(lines_until_stop)
print(*d, sep='\n')
您可以通过使用reversed
反转所有行来简化所有操作。
>>> art = '''
... *
... ***
... *****
... ***
... ***
... '''
>>> print('\n'.join(reversed(art.splitlines())))
***
***
*****
***
*
我今天很慷慨,因此举一个完整的例子:
text = ""
stopword = "END"
lines = []
while True:
line = input()
if line.strip() == stopword:
break
lines.append(line)
print('\n'.join(reversed(lines)))
for item in lines[::-1]:
print item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.