简体   繁体   English

在某些方向打印嵌套列表 - Python

[英]Printing nested lists in certain directions - Python

I am trying a problem to make Graphical designs in Python, more specifically ASCII "banner" words. 我正在尝试用Python制作图形设计的问题,更具体地说是ASCII“横幅”字。 Each letter is made up of a nested list of lines of the character. 每个字母由字符的嵌套列表组成。

 [[' _______ ', '(       )', '| () () |', '| || || |', '| |(_)| |', '| |   | |', '| )   ( |', '|/     \\|'], [' _______ ', '(  ____ \\', '| (    \\/', '| (__    ', '|  __)   ', '| (      ', '| (____/\\', '(_______/']]

etc. 等等

When printed down for each nested list and across for the whole thing, they make a word. 当为每个嵌套列表向下打印并为整个事物打印时,它们会形成一个单词。 I am having trouble printing it as I said above, down for each nested list and across for the whole thing. 我正如上面所说的那样打印它时遇到问题,对于每个嵌套列表都是如此。 Thanks in advance! 提前致谢!

If you want to print the letters from left to right, you will have to zip the list of lists with itself, effectively "transposing" it. 如果你想从左到右打印字母,你必须自己zip列表列表,有效地“转置”它。 This way, the first list will have all the first rows, the second list all the second rows, and so on. 这样,第一个列表将包含所有第一行,第二个列表包含所有第二行,依此类推。 Now just join those and you are done. 现在join这些,你就完成了。

>>> ascii = [[' _______ ', '(       )', '| () () |', '| || || |', '| |(_)| |', '| |   | |', '| )   ( |', '|/     \\|'], [' _______ ', '(  ____ \\', '| (    \\/', '| (__    ', '|  __)   ', '| (      ', '| (____/\\', '(_______/']]
>>> print '\n'.join((' '.join(line) for line in zip(*ascii))) 
 _______   _______ 
(       ) (  ____ \
| () () | | (    \/
| || || | | (__    
| |(_)| | |  __)   
| |   | | | (      
| )   ( | | (____/\
|/     \| (_______/

And if you want to print the letters from top to bottom, you can use this: 如果你想从上到下打印字母,你可以使用:

>>> print '\n\n'.join(('\n'.join(line) for line in ascii))

Although it's not exactly clear what output you want, I believe you could benefit from using the string.join() function. 虽然不清楚你想要什么输出,但我相信你可以从使用string.join()函数中受益。

EDIT 编辑

Aha, it's now clear what you are trying to do. 啊哈,现在很清楚你想要做什么。 Using the solution of @tobias_k I would suggest that you make it easy to edit your letters by doing something like this: 使用@tobias_k的解决方案,我建议你通过这样的方式轻松编辑你的字母:

m = """
 _______  
(       ) 
| () () | 
| || || | 
| |(_)| | 
| |   | | 
| )   ( | 
|/     \| 
""".split('\n')

e = """
 _______  
(  ____ \ 
| (    \/ 
| (__     
|  __)    
| (       
| (____/\ 
(_______/ 
""".split('\n')

lines = zip(m,e,m,e)   # Spell the word you want here
print '\n'.join(' '.join(line) for line in lines)

Gives output: 给出输出:

 _______    _______    _______    _______  
(       )  (  ____ \  (       )  (  ____ \ 
| () () |  | (    \/  | () () |  | (    \/ 
| || || |  | (__      | || || |  | (__     
| |(_)| |  |  __)     | |(_)| |  |  __)    
| |   | |  | (        | |   | |  | (       
| )   ( |  | (____/\  | )   ( |  | (____/\ 
|/     \|  (_______/  |/     \|  (_______/ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM