循环工作时间太长

时间:2013-07-30 14:01:01

标签: python algorithm

我有两个词组列表:prices_distinctsprices

他们通过hash_brand_artnum连接,两者都按hash_brand_artnum排序  我不明白为什么循环工作这么久:

  1. 如果prices_distincts的长度为100,000,则适用于30 min

  2. 但如果prices_distincts的长度为10,000,则适用于10 sec

  3. 代码:

     prices_distincts = [{'hash_brand_artnum':1202},...,..]
     prices = [{'hash_brand_artnum':1202,'price':12.077},...,...]
    
     for prices_distinct in prices_distincts:
        for price in list(prices):            
            if prices_distinct['hash_brand_artnum'] == price['hash_brand_artnum']:
    
                print price['hash_brand_artnum']
                #print prices
                del prices[0]
            else:
                continue
    

    我需要查找价格相同的商品。 price_distincts和价格之间的关系是一对多的。和价格相同的团体价格['hash_brand_artnum']

3 个答案:

答案 0 :(得分:10)

它的工作时间很长,因为你的算法是O(N ^ 2)和100000 ^ 2 = 10000000000和10000 ^ 2 = 100000000.因此,两个数之间的因子是100,并且因子介于30分钟和10秒~100之间。 / p>

编辑:您的代码和如此少量的数据很难说,我不知道您的任务是什么,但我认为您的词典不是很有用。 可以试试这个:

>>> prices_distincts = [{'hash_brand_artnum':1202}, {'hash_brand_artnum':14}]
>>> prices = [{'hash_brand_artnum':1202, 'price':12.077}, {'hash_brand_artnum':14, 'price':15}]
# turning first list of dicts into simple list of numbers
>>> dist = [x['hash_brand_artnum'] for x in prices_distincts]
# turning second list of dicts into dict where number is a key and price is a value
>>> pr = {x['hash_brand_artnum']:x["price"] for x in prices}

不是你可以通过你的号码迭代并获得价格:

>>> for d in dist:
...     print d, pr[d]

答案 1 :(得分:5)

正如@RomanPekar所提到的,您的算法运行缓慢,因为它的复杂性为O(n^2)。要修复它,您应该将其写为O(n)算法:

import itertools as it

for price, prices_distinct in it.izip(prices, prices_distincts):
    if prices_distinct['hash_brand_artnum'] == price['hash_brand_artnum']:
        # do stuff

答案 2 :(得分:0)

如果price_distincts或多或少价格增长,那么如果你将prices_distincts的大小乘以10,你的原始10秒将乘以10然后再乘以10(循环的第二个),然后乘以~2,因为“列表(价格)”(顺便说一下,应该最终完成循环):

10秒* 10 * 10 * 2 = 2000秒= 33分钟

这种转换通常很昂贵。

 prices_distincts = [{'hash_brand_artnum':1202},...,..]
 prices = [{'hash_brand_artnum':1202,'price':12.077},...,...]
 list_prices = list(prices)

 for prices_distinct in prices_distincts:
    for price in list_prices:            
        if prices_distinct['hash_brand_artnum'] == price['hash_brand_artnum']:

            print price['hash_brand_artnum']
            #print prices
            del prices[0]
        else:
            continue