簡體   English   中英

嵌套循環可處理單個字典

[英]Nested loops to process a single dictionary

我正在嘗試根據他們合作的電影數量來建立演員/女演員的圖表。 我提取了一個字典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個actor / actress。 得到結果可能需要幾天的時間。

因此,我可以做些什么來加速我的代碼。

編輯:

我意識到我需要提供更多有關我的問題的信息。

假設我有一個僅由三個演員組成的people_dict ,因為284602都曾在電影'Electric Daisy Carnival Experience (2011)' people_dict ,並且與此同時,對於660和另外兩個人沒有共同的電影,那么我只是期望284602會被寫入類似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)']}
  • 每次都將兩個列表變成一組-為什么不從一開始就將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毫秒內完成

(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毫秒內完成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM