简体   繁体   中英

How to print list of lists without extra brackets and quotes?

I'm working on assignment for my Python 3 programming class. It's a database to look up movies and the year they came out. However, I'm having a hard time printing the output without extra brackets and quotes:

# 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

I would like this output to be:

2005:
    Munich, Steven Spielberg

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

etc.

The output I am getting:

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

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

etc.

Replace

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

with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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