如果值是列表,则反转字典中的键和值

问题描述 投票:0回答:1

我试图反转由值列表创建的字典中的键和值。我该如何处理这个问题? 这是作业:

编写Python程序,使用该文件中的数据,并创建一个字典,其中男演员/女演员的名字作为键,值是该男演员/女演员的电影列表。创建字典后,您的程序应询问用户演员的姓名,然后使用字典中的信息打印该演员的电影。

in_file = open("movie and actors.txt", "r")

movie_dictionary = {}
for line in in_file:
    movies_and_actors = line.strip().split(', ')
    movie = movies_and_actors[0]
    actor = movies_and_actors[1:]
    movie_dictionary[movie] = actor

s = input("Enter an actor/actress name: ")
d = [i for i in movie_dictionary if s in movie_dictionary[i]]
listToStr = ' and '.join([elem for elem in d])
print("The movies that", s, "starred in are", listToStr)

in_file.close()

电影和演员.txt

La La Land, Ryan Gosling, Emma Stone
Blade Runner 2049, Harrison Ford, Ryan Gosling, Ana de Armas
Cruella, Emma Stone, Emma Thompson, Joel Fry
Iron Man, Robert Downey Jr., Gwyneth Paltrow, Terrence Howard
Avatar, Sam Worthington, Zoe Saldana, Sigourney Weaver
Star Wars A New Hope, Mark Hamil, Carrie Fisher, Harrison Ford
Air Force One, Harrison Ford, Gary Oldman, Glen Close
Pretty Woman, Richard Gere, Julia Roberts, Jason Alexander
My Best Friend's Wedding, Julia Roberts, dermont Multroney, 
Cameron Diaz
There's Something About Mary, Cameron Diaz, Matt Dillon, Ben 
Stiller
Meet the Parents, Ben Stiller, Robert De Niro, Teri Polo
Top Gun Maverick, Tom Cruse, Jennifer Connelly, Miles Teller
Black Panther: Wakanda Forever, Letitia Wright, Lupita Nyong'o, 
Danai Gurira
Thor: Love and Thunder, Chris Hemsworth, Natalie Portman, 
Christian Bale
Notting Hill, Hugh Grant, Julia Roberts, Richard McCabe
python dictionary key
1个回答
0
投票

假设这不是数据清理练习,并且您的数据准确地表示为:

rows = """
La La Land, Ryan Gosling, Emma Stone
Blade Runner 2049, Harrison Ford, Ryan Gosling, Ana de Armas
Cruella, Emma Stone, Emma Thompson, Joel Fry
Iron Man, Robert Downey Jr., Gwyneth Paltrow, Terrence Howard
Avatar, Sam Worthington, Zoe Saldana, Sigourney Weaver
Star Wars A New Hope, Mark Hamil, Carrie Fisher, Harrison Ford
Air Force One, Harrison Ford, Gary Oldman, Glen Close
Pretty Woman, Richard Gere, Julia Roberts, Jason Alexander
My Best Friend's Wedding, Julia Roberts, dermont Multroney, Cameron Diaz
There's Something About Mary, Cameron Diaz, Matt Dillon, Ben Stiller
Meet the Parents, Ben Stiller, Robert De Niro, Teri Polo
Top Gun Maverick, Tom Cruse, Jennifer Connelly, Miles Teller
Black Panther: Wakanda Forever, Letitia Wright, Lupita Nyong'o, Danai Gurira
Thor: Love and Thunder, Chris Hemsworth, Natalie Portman, Christian Bale
Notting Hill, Hugh Grant, Julia Roberts, Richard McCabe
""".strip().split("\n")

然后您可以一一处理这些行并捕获电影和演员列表。然后,您可以迭代参与者列表并使用字典的

setdefault()
方法来确保新参与者键正确初始化为空列表以附加到:

appears_in = {}
for row in rows:
    movie, *actors = row.split(",")
    for actor in actors:
        appears_in.setdefault(actor.strip(), []).append(movie.strip())

最后我们可以使用演员的名字来打印他们的电影:

actor = "Harrison Ford"
print(f"{actor} has appeared in {', '.join(appears_in[actor])}")

结果应该是:

Harrison Ford has appeared in Blade Runner 2049, Star Wars A New Hope, Air Force One

另一方面,如果此练习涉及数据清理步骤,因为您的数据实际上如您所述:

rows = """
La La Land, Ryan Gosling, Emma Stone
Blade Runner 2049, Harrison Ford, Ryan Gosling, Ana de Armas
Cruella, Emma Stone, Emma Thompson, Joel Fry
Iron Man, Robert Downey Jr., Gwyneth Paltrow, Terrence Howard
Avatar, Sam Worthington, Zoe Saldana, Sigourney Weaver
Star Wars A New Hope, Mark Hamil, Carrie Fisher, Harrison Ford
Air Force One, Harrison Ford, Gary Oldman, Glen Close
Pretty Woman, Richard Gere, Julia Roberts, Jason Alexander
My Best Friend's Wedding, Julia Roberts, dermont Multroney, 
Cameron Diaz
There's Something About Mary, Cameron Diaz, Matt Dillon, Ben 
Stiller
Meet the Parents, Ben Stiller, Robert De Niro, Teri Polo
Top Gun Maverick, Tom Cruse, Jennifer Connelly, Miles Teller
Black Panther: Wakanda Forever, Letitia Wright, Lupita Nyong'o, 
Danai Gurira
Thor: Love and Thunder, Chris Hemsworth, Natalie Portman, 
Christian Bale
Notting Hill, Hugh Grant, Julia Roberts, Richard McCabe
""".strip().split("\n")

然后我们可以迭代行,查找只有一个“条目”的行,如果找到一个,则将其附加到前一行。

rows_fixed = []
prior_row = ""
for row in rows:
    parts = row.split(",")
    if len(parts) == 1:
        prior_row += row
        continue
    if prior_row:
        rows_fixed.append(prior_row)

    prior_row = row

if prior_row:
    rows_fixed.append(prior_row)

rows = rows_fixed
for row in rows:
    print(row)

这应该会给你一个类似的结果:

La La Land, Ryan Gosling, Emma Stone
Blade Runner 2049, Harrison Ford, Ryan Gosling, Ana de Armas
Cruella, Emma Stone, Emma Thompson, Joel Fry
Iron Man, Robert Downey Jr., Gwyneth Paltrow, Terrence Howard
Avatar, Sam Worthington, Zoe Saldana, Sigourney Weaver
Star Wars A New Hope, Mark Hamil, Carrie Fisher, Harrison Ford
Air Force One, Harrison Ford, Gary Oldman, Glen Close
Pretty Woman, Richard Gere, Julia Roberts, Jason Alexander
My Best Friend's Wedding, Julia Roberts, dermont Multroney, Cameron Diaz
There's Something About Mary, Cameron Diaz, Matt Dillon, Ben Stiller
Meet the Parents, Ben Stiller, Robert De Niro, Teri Polo
Top Gun Maverick, Tom Cruse, Jennifer Connelly, Miles Teller
Black Panther: Wakanda Forever, Letitia Wright, Lupita Nyong'o, Danai Gurira
Thor: Love and Thunder, Chris Hemsworth, Natalie Portman, Christian Bale
Notting Hill, Hugh Grant, Julia Roberts, Richard McCabe
© www.soinside.com 2019 - 2024. All rights reserved.