使用大量数据处理Python中的字典

时间:2018-05-27 21:16:18

标签: python python-3.x dictionary

我现在正试图用Python字典处理IMDb数据。在清理了一些基本数据后,我有一个字典people_dict,看起来像

people_dict = {...,936: ['And White Was the Night (2015)', 'Lipton Cockton in the Shadows of Sodoma (1995)', 'Maraton (1997)', 'Rundi (1990)', 'Sounds Like Suomi (2008)'],...}

其中键代表演员/女演员的id,而列表是他/她所扮演的一组电影。

现在我正在尝试根据movie_dict获取另一个字典people_dict,它看起来像

movie_dict = {...,'Beats, Rhymes & Life: The Travels of a Tribe Called Quest (2011)': [3],...}

其中键是电影的名称,而值是演员/女演员id。 但是,我的实现(见下文)是嵌套循环,但涉及近万个电影和演员/女演员。它乐观地可以在一周内给出我想要的东西。

for value in movie_dict.keys():
    for people_id, movie_list in people_dict.items():
        if value in movie_list:
            movie_dict[value].append(people_id)

所以我可以做些什么来显着减少运行时间。我已经查看了this thread,其中地图似乎是一个不错的选择。

0 个答案:

没有答案