python function slowing down for no apparent reason

后端 未结 6 1016
小鲜肉
小鲜肉 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:38

    EDIT: I've updated my answer to account for lists being unhashable, as well as some other feedback. This one is even tested.

    It probably relates to the cost of poping an item out of a middle of a list.

    Alternatively have you tried using sets to handle this?

    def difference(list1, list2):
        return [x for x in list1 if tuple(x) in set(tuple(y) for y in list2)]
    

    You can then set list one to the resulting list if that is your intention by doing

    list1 = difference(list1, list2)
    

提交回复
热议问题