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

时间:2017-10-18 02:16:39

标签: python-3.x dictionary printing

这是我的代码的缩短版本。基本上,我有一个字典存储包含我的ascii字母的列表。我提示用户选择字母然后用特殊设计打印出来。第二个用户输入用于决定如何打印字母。 'h'=水平,'v'=垂直。垂直部分完美运作。横向没有。

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:
 _______ 
 _______ 
(  ___  )
(  ___  )
| (   ) |
| (   ) |
| (___) |
| (___) |
|  ___  |
|  ___  |
| (   ) |
| (   ) |
| )   ( |
| )   ( |
|/     \|
|/     \|

1 个答案:

答案 0 :(得分:0)

您可以zip将这些线组合在一起然后加入它们:

if direction == "h":
    zipped = zip(*(ascii_letter[char] for char in input_string))
    for line in zipped:
        print(' '.join(line))

或者只是索引:

if direction == "h":
    for i in range(8):
        for char in input_string:
            print(ascii_letter[char][i], end=' ')
        print()
相关问题