如何在没有额外括号和引号的情况下打印列表列表?

时间:2017-12-07 18:20:21

标签: python python-3.x

我正在为我的Python 3编程课工作。它是一个查找电影及其出现年份的数据库。但是,我很难打印输出而没有额外的括号和引号:

# Build a dictionary containing the specified movie collection
list_2005 = [["Munich", "Steven Spielberg"]]
list_2006 = [["The Departed", "Martin Scorsese"], ["The Prestige", "Christopher Nolan"]]
list_2007 = [["Into the Wild", "Sean Penn"]]

movies = {
'2005': list_2005, 
'2006': list_2006, 
'2007': list_2007
      }

# Prompt the user for a year 
# Displaying the title(s) and directors(s) from that year
user_year = str(input("Enter a year between 2005 and 2007:\n"))

if user_year in movies:

    for name in movies[user_year]:
        print("%s" % ', '.join(name))
    print()

elif user_year not in movies:
    print("N/A")

# Display menu
user_choice = ''

while user_choice != 'q':
    print("MENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit")
    print()
    user_choice = str(input("Choose an option:\n"))

    if user_choice == 'y':
       for key, value in sorted(movies.items()):
           print("%s:" % key)
           print("    %s" % ''.join(str(movies[key])))


# Carry out the desired option: Display movies by year, 
# display movies by director, display movies by movie title, or quit

我希望这个输出为:

2005:
    Munich, Steven Spielberg

2006:
    The Prestige, Christopher Nolan
    The Departed, Martin Scorsese

我得到的输出:

2005:
    ['Munich', 'Steven Spielberg']

2006:
    [['The Prestige', 'Christopher Nolan'], ['The Departed', 'Martin Scorsese']]

1 个答案:

答案 0 :(得分:0)

替换

print(" %s" % ''.join(str(movies[key])))

print("\t" + '\n\t'.join("{}, {}".format(m[0], m[1]) for m in movies[key]))