首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python,从字典中打印单独的索引

Python,从字典中打印单独的索引
EN

Stack Overflow用户
提问于 2017-10-18 02:16:39
回答 1查看 23关注 0票数 0

下面是我代码的简写版本。基本上,我有一本字典,里面有一个包含我的信的列表。我正在提示用户选择字母,然后用特殊的设计打印出来。第二个用户输入用于决定如何打印字母。'h‘=水平,'v’=垂直。垂直部分工作得很好。水平没有。

代码语言:javascript
复制
def print_banner(input_string, direction):
'''
Function declares a list of ascii characters, and arranges the characters in order to be printed.
'''
ascii_letter = {'a': [" _______ ",
                      "(  ___  )",
                      "| (   ) |",
                      "| (___) |",
                      "|  ___  |",
                      "| (   ) |",
                      "| )   ( |",
                      "|/     \|"]}
    # Branch that encompasses the horizontal banner
if direction == "h":
    # Letters are 8 lines tall
    for i in range(8):
        for letter in range(len(input_string)):
            # Dict[LetterIndex[ListIndex]][Line]
            print(ascii_letter[input_string[letter]][i], end=" ")
            print(" ")

# Branch that encompasses the vertical banner
elif direction == "v":
    for letter in input_string:
        for item in ascii_letter[letter]:
            print(item)


def main():
    user_input = input("Enter a word to convert it to ascii: ").lower()
    user_direction = input("Enter a direction to display: ").lower()
    print_banner(user_input, user_direction)

# This is my desired output if input is aa
_______   _______ 
(  ___  ) (  ___  )
| (   ) | | (   ) |
| (___) | | (___) |
|  ___  | |  ___  |
| (   ) | | (   ) |
| )   ( | | )   ( |
|/     \| |/     \|

#What i get instead is:
 _______ 
 _______ 
(  ___  )
(  ___  )
| (   ) |
| (   ) |
| (___) |
| (___) |
|  ___  |
|  ___  |
| (   ) |
| (   ) |
| )   ( |
| )   ( |
|/     \|
|/     \|
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-18 04:18:54

您可以将这些行zip在一起,然后加入它们:

代码语言:javascript
复制
if direction == "h":
    zipped = zip(*(ascii_letter[char] for char in input_string))
    for line in zipped:
        print(' '.join(line))

或者只是索引:

代码语言:javascript
复制
if direction == "h":
    for i in range(8):
        for char in input_string:
            print(ascii_letter[char][i], end=' ')
        print()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46801837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档