python function slowing down for no apparent reason

后端 未结 6 1023
小鲜肉
小鲜肉 2021-01-22 16:56

I have a python function defined as follows which i use to delete from list1 the items which are already in list2. I am using python 2.6.2 on windows XP

def comp         


        
6条回答
  •  不知归路
    2021-01-22 17:26

    There are 2 issues that cause your algorithm to scale poorly:

    1. x in list is an O(n) operation.
    2. pop(n) where n is in the middle of the array is an O(n) operation.

    Both situations cause it to scale poorly O(n^2) for large amounts of data. gnud's implementation would probably be the best solution since it solves both problems without changing the order of elements or removing potential duplicates.

提交回复
热议问题