Compare two lists, find old (removed) and new (added) values

时间:2015-05-12 22:32:16

标签: python list python-2.7

Say I have the following, a list:

var params = $(row_id).find('input, select, textarea').serializeArray();

console.log(params);

At a point later, it's updated and might become, for example:

A
B
C
D
E
F

I need to find which new elements have entered the list, but ALSO which old elements have left the list, upon update. Of course this can be done with brute force (and a fair bit of code), but what would be the "standardised" (i.e sagacious) method for completing this task, minimally, with output as:

Entered list = G, H, I

Exited list = E, F

4 个答案:

答案 0 :(得分:2)

I'd just use sets, they make lookups fast:

SELECT

Depends on your actual data, though, like whether you have duplicates (and if yes, how you want them treated) and whether you care about order and whether the list elements are hashable.

答案 1 :(得分:2)

You said it can be done with brute force and it doesn't really seem like you want that, but it can be done with list comprehension in two lines.

publish

This checks each item in each list and sees if it is in the other list. It then filters out which ones are not and puts them in a new list. The only possible problem with this is that each list check has a complexity of entered = [i for i in x if i not in y] exited = [i for i in y if i not in x] , so in total that is O(n).

答案 2 :(得分:0)

This might help you :

.box-detail {
    display: inline-block;
    position: absolute;
    height: 100%;
    width: 100%;
    transition: 0.3s;
    z-index: 1000;
    transform:scale(0,0);
}

.box-reveal-prop {
    transform:scale(1,1);
}

答案 3 :(得分:0)

You can use difflib module.Check the output for '+' and '-'.

You can make a deepcopy of of original list and compare the subsequent modified list

=SQRT((INDEX($A$2:$A$33,1+QUOTIENT(ROWS(A$2:A2)-1,8)*8)-A2)^2+(INDEX($B$2:$B$33,1+QUOTIENT(ROWS(B$2:B2)-1,8)*8)-B2)^2+(INDEX($C$2:$C$33,1+QUOTIENT(ROWS(C$2:C2)-1,8)*8)-C2)^2)