我正在尝试根据他们合作过多少部电影来制作演员/女演员的图表。我提取了一个字典people_dict
,看起来像:
people_dict = {..., 3: ['American Pimp (1999)',
'Beats, Rhymes & Life: The Travels of a Tribe Called Quest (2011)',
'Gangsta Rap: The Glockumentary (2007)',
'Ghetto Physics (2010)',
'Mac Dre: Legend of the Bay (2014)',
'Menace II Society (1993)',
'Pimpalation: Return of the Trill (2006)',
'Porndogs: The Adventures of Sadie (2009)',
'Rhyme & Reason (1997)',
'Stop Pepper Palmer (2014)',
'Townbiz (2010)',
'Uprising: Hip Hop and the LA Riots (2012)'],...}
其中key
是演员/演员ID,value
是他/她所演过的所有电影的列表。
现在我想找到演员/女演员之间的合作,因此我需要计算不同键值之间的交集,如下所示:
for people_id, movie_list in people_dict.items():
for other_people_id, other_movie_list in people_dict.items():
if people_id < other_people_id:
intersection = set(movie_list) & set(other_movie_list)
if intersection:
edge_list.append((people_id, other_people_id))
但是,我的实现是嵌套循环,这本词典中有超过100,000名演员/女演员。结果可能需要几天时间。
我可以做些什么来加速我的代码。
编辑:
我意识到我需要提供更多关于我的问题的内容。
假设我有people_dict
只有三位演员/演员,因为284
和602
都在电影'Electric Daisy Carnival Experience (2011)'
中演出,但同时也没有共同点660
和其他两个人的电影,我只希望将284
和602
写在284 602
这样的文本文件中。
people_dict = {284: ['DGK: Parental Advisory (2012)',
'Electric Daisy Carnival Experience (2011)',
'Malibu Horror Story (2014)'],
602: ['5 Sides of a Coin (2003)',
'As I AM: The Life and Times of DJ AM (2015)',
'Electric Daisy Carnival Experience (2011)',
'Hang the DJ (1998)'],
661: ['Every Boy Needs a Father (2013)',
'The Sketches of Matty Mad Man: Part Dos (2014)',
'Trippy: Spitting Rainbows (2014)',
'Vicky Foxx: Forever in Blue Jeans (2010)']}
答案 0 :(得分:0)
每次都将两个列表设置为集合 - 为什么不从一开始就将movie_list设置为一组?
无论如何,主要问题是复杂度O(n ^ 2)。 你真的需要将每个项目相互比较两次吗?
也许这会引发一些想法:
from collections import defaultdict
import functools
import time
import random
movies = range(100)
actors = range(1000)
def get_random_movies(max=10):
return random.sample(movies, random.randint(1, max))
people_dict = {actor_id: get_random_movies() for actor_id in actors}
def timeit(func):
@functools.wraps(func)
def newfunc(*args, **kwargs):
start_time = time.time()
func(*args, **kwargs)
elapsed_time = time.time() - start_time
print('function [{}] finished in {} ms'.format(
func.__name__, int(elapsed_time * 1000)))
return newfunc
@timeit
def usingDefaultDict():
md = defaultdict(set)
for k, v in people_dict.items():
for m in v:
md[m].add(k)
collaborations = {
actor_id: set([aid for actors in md.values() for aid in actors if actor_id in actors])
for actor_id in people_dict}
print(next(iter(collaborations.items())))
@timeit
def usingNestedLoops():
edge_list = []
for people_id, movie_list in people_dict.items():
for other_people_id, other_movie_list in people_dict.items():
if people_id < other_people_id:
intersection = set(movie_list) & set(other_movie_list)
if intersection:
edge_list.append((people_id, other_people_id))
collaborations = {
actor_id: set([aid for actors in edge_list for aid in actors if actor_id in actors])
for actor_id in people_dict}
print(next(iter(collaborations.items())))
usingNestedLoops()
usingDefaultDict()
导致集合的字典:{actor_id:actors_collaborated_with}
(0, {0, 1, 512, 516, 519, 8, 520, 522, 12, 524, 14, 15, 529, 18, 532, 533, 534, 535, 26, 28, 29, 30, 31, 540, 545, 35, 36, 38, 39, 40, 43, 47, 48, 49, 50, 563, 564, 54, 568, 57, 58, 572, 63, 64, 65, 576, 67, 69, 70, 581, 582, 73, 74, 75, 583, 77, 587, 81, 594, 597, 86, 89, 605, 606, 97, 610, 100, 101, 617, 107, 621, 110, 622, 623, 629, 118, 121, 635, 124, 639, 129, 130, 131, 132, 133, 135, 650, 139, 140, 141, 653, 143, 655, 146, 147, 658, 660, 151, 154, 666, 669, 158, 160, 161, 673, 675, 164, 165, 166, 680, 169, 684, 175, 687, 177, 181, 693, 183, 184, 185, 698, 189, 190, 704, 705, 195, 197, 709, 710, 201, 713, 714, 715, 205, 718, 721, 210, 723, 213, 214, 215, 730, 222, 734, 735, 225, 737, 228, 230, 743, 232, 746, 748, 238, 240, 753, 243, 755, 246, 761, 762, 251, 763, 253, 766, 255, 767, 769, 258, 259, 261, 775, 776, 777, 779, 270, 273, 274, 275, 786, 277, 278, 787, 792, 281, 282, 795, 286, 287, 292, 294, 299, 813, 815, 304, 817, 308, 309, 821, 311, 824, 826, 828, 829, 830, 319, 321, 837, 328, 840, 331, 334, 846, 847, 849, 339, 340, 851, 342, 854, 344, 855, 352, 867, 868, 359, 872, 362, 363, 366, 367, 880, 882, 883, 372, 373, 886, 890, 892, 384, 387, 390, 902, 903, 393, 394, 908, 399, 913, 916, 918, 921, 411, 412, 421, 423, 936, 937, 426, 938, 430, 431, 432, 433, 942, 949, 950, 951, 952, 444, 445, 447, 960, 450, 964, 454, 457, 460, 461, 462, 463, 464, 972, 981, 471, 983, 986, 991, 995, 484, 997, 998, 487, 489, 495, 499, 502})
函数[usingNestedLoops]在66736 ms完成
(0, {0, 1, 512, 516, 519, 8, 520, 522, 12, 524, 14, 15, 529, 18, 532, 533, 534, 535, 26, 540, 29, 30, 31, 28, 545, 35, 36, 38, 39, 40, 43, 47, 48, 49, 50, 563, 564, 54, 568, 57, 58, 572, 63, 576, 65, 64, 67, 581, 582, 583, 70, 73, 74, 587, 69, 77, 75, 81, 594, 597, 86, 89, 605, 606, 97, 610, 100, 101, 617, 107, 621, 622, 623, 110, 629, 118, 121, 635, 124, 639, 129, 130, 131, 132, 133, 135, 650, 139, 140, 653, 141, 143, 655, 658, 146, 147, 660, 151, 154, 666, 669, 158, 160, 161, 673, 675, 164, 165, 166, 680, 169, 684, 175, 687, 177, 181, 693, 183, 184, 185, 698, 189, 190, 704, 705, 195, 197, 709, 710, 713, 714, 201, 715, 205, 718, 721, 210, 723, 213, 214, 215, 730, 734, 735, 222, 737, 225, 228, 230, 743, 232, 746, 748, 238, 240, 753, 755, 243, 246, 761, 762, 251, 763, 253, 766, 255, 767, 769, 258, 259, 261, 775, 776, 777, 779, 270, 273, 786, 274, 275, 277, 278, 787, 792, 281, 282, 795, 286, 287, 292, 294, 299, 813, 815, 304, 817, 308, 821, 309, 311, 824, 826, 828, 829, 830, 319, 321, 837, 840, 328, 331, 846, 847, 334, 849, 339, 851, 340, 342, 854, 344, 855, 352, 867, 868, 359, 872, 362, 363, 366, 367, 880, 882, 883, 372, 373, 886, 890, 892, 384, 387, 902, 903, 390, 393, 394, 908, 399, 913, 916, 918, 921, 411, 412, 421, 423, 936, 937, 426, 938, 430, 942, 432, 433, 431, 949, 950, 951, 952, 444, 445, 447, 960, 450, 964, 454, 457, 972, 460, 462, 461, 464, 463, 981, 471, 983, 986, 991, 995, 484, 997, 998, 487, 489, 495, 499, 502})
函数[usingDefaultDict]在816 ms完成