【问题标题】:File to a setted dictionary文件到设置的字典
【发布时间】:2016-12-16 19:42:23
【问题描述】:

我需要将此文件内容转换为字典,以便字典中的每个键都是电影的名称,每个值都是在集合中播放的演员的名称。 我考虑阅读文件,将每部电影名称放在一个列表中,然后将其设置为删除重复项。 然后,一个循环让每部电影的名字都成为一个键,问题是下一步是什么?怎么办 我让价值观演员的名字?

注释:它是一个文本文件 (.txt) 文件内容示例:

Brad Pitt, Sleepers, Troy, Meet Joe Black, Oceans Eleven, Seven, Mr & Mrs Smith
Tom Hanks, You have got mail, Apollo 13, Sleepless in Seattle, Catch Me If You Can
Meg Ryan, You have got mail, Sleepless in Seattle
Diane Kruger, Troy, National Treasure
Dustin Hoffman, Sleepers, The Lost City
Anthony Hopkins, Hannibal, The Edge, Meet Joe Black, Proof
Alec Baldwin, The Edge, Pearl Harbor
Angelina Jolie, Bone Collector, Lara Croft Tomb Raider, Mr & Mrs Smith
Denzel Washington, Bone Collector, The Siege, American Gangster
Julia Roberts, Pretty Woman, Oceans Eleven, Runaway Bride
Gwyneth Paltrow, Shakespeare in Love, Bounce, Proof
Russell Crowe, Gladiator, A Beautiful Mind, Cinderella Man, American Gangster
Sylvester Stallone, Rocky, Rambo, Assassins
Johnny Depp, Edward Scissorhands, The Pirates of Caribbean, Finding Neverland
Leonardo Di Caprio, Titanic, Blood Diamond, The Departed, Catch Me If You Can
Antonio Banderas, The Mask of Zorro, Desperado
Tom Cruise, Top Gun, Mission Impossible, Jerry Maguire, A Few Good Men
Kate Winslet, Titanic, Finding Neverland
George Clooney, Oceans Eleven, Intolerable Cruelty
Matt Damon, Good Will Hunting, Bourne Identity, Bourne Ultimatum, The Departed
Ben Affleck, Bounce, Good Will Hunting, Pearl Harbor
Catherine Zeta Jones, The Mask of Zorro, Intolerable Cruelty
Morgan Freeman, Bone Collector, Seven, Million Dollar Baby, Bruce Almighty
Bruce Willis, Die Hard, The Sixth Sense, Pulp Fiction, The Siege
Julianne Moore, Assassins, Hannibal
Salma Hayek, Desperado, Wild Wild West
Will Smith, Wild Wild West, Pursuit of Happyness, Hitch, Men in Black
Kevin Bacon, A Few Good Men, Sleepers
Jim Carrey, The Mask, Bruce Almighty, Ace Ventura, Me-Myself & Irene
Renee Zellweger, Me-Myself & Irene, Jerry Maguire, Cinderella Man

【问题讨论】:

  • 请更详细一点

标签: python python-2.7 dictionary


【解决方案1】:

即使输入是文本文件,也可以使用 Python csv 模块读取它,因为它的格式似乎是正确的,这样做可以相对简单地处理数据和创建字典本身。

格式化结果有点曲折,因为我希望它只是某种方式——但这不是你要问的,对吧? ;-)

#!/usr/bin/env python2
import csv

# Create and populate the target dictionary from the data in the file.
movie_actors_dict = {}
with open('actors.txt', 'rb') as csvfile:
    for row in csv.reader(csvfile, skipinitialspace=True):
        actor, movies = row[0], row[1:]
        for movie in movies:
            movie_actors_dict.setdefault(movie, set()).add(actor)

# Display resulting dictionary.
print('{')
representation = ',\n'.join(('    {!r}: {{\n'.format(movie)
                             + ',\n'.join('        {!r}'.format(actor)
                                for actor in sorted(movie_actors_dict[movie]))
                             + '\n    }') for movie in sorted(movie_actors_dict))
print(representation)
print('}')

样本输出:

{
    'A Beautiful Mind': {
        'Russell Crowe'
    },
    'A Few Good Men': {
        'Kevin Bacon',
        'Tom Cruise'
    },
    'Ace Ventura': {
        'Jim Carrey'
    },
    'American Gangster': {
        'Denzel Washington',
        'Russell Crowe'
    },
    'Apollo 13': {
        'Tom Hanks'
    },
    'Assassins': {
        'Julianne Moore',
        'Sylvester Stallone'
    },
    'Blood Diamond': {
        'Leonardo Di Caprio'
    },
    'Bone Collector': {
        'Angelina Jolie',
        'Denzel Washington',
        'Morgan Freeman'
    },
    ... etc, etc
}

【讨论】:

  • 我使用的是 python 2,所以这段代码似乎不起作用(因为 *movies),你能否解释一下这一行“for actor, *movies in csv.reader( csvfile, skipinitialspace=True):"
  • 您应该已经标记(并且可能在您的问题中提到)您使用的版本,因为您所询问的所有内容都与版本 3 中的新内容有关。您为什么使用版本 2 ?
  • @martineau OP 已将该问题标记为 python-2.7
  • @shash678:不,我是在他们让我知道后才这样做的(并相应地修改了我的答案)。
猜你喜欢
  • 2011-02-22
  • 2021-05-03
  • 2016-03-08
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多