Difference Between Two Lists

Get difference between two lists

To get elements which are in temp1 but not in temp2 :

In [5]: list(set(temp1) - set(temp2))
Out[5]: ['Four', 'Three']

Beware that it is asymmetric :

In [5]: set([1, 2]) - set([2, 3])
Out[5]: set([1])

where you might expect/want it to equal set([1, 3]). If you do want set([1, 3]) as your answer, you can use set([1, 2]).symmetric_difference(set([2, 3])).

How to get difference between two lists in dart?

You can loop through one of the loop and then check if the item is present in the other list or not.

void main() {
List x = ['one' , 'two' , 'three' , 'four'];
List y = ['one' , 'two',];
List output = [];

for(final e in x){
bool found = false;
for(final f in y) {
if(e == f) {
found = true;
break;
}
}
if(!found){
output.add(e);
}
}
print(output);
}

Difference between two lists of lists in Python

Just use list comprehensions like so:

a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[1,2,3],[9,9,9]]
c = [d for d in a if d not in b]
print(c)

Output:

[[4, 5, 6], [7, 8, 9]]

How can I get the difference between two lists?

The other solution runs in O(N^2), where as this solution runs in O(N + M) time complexity.

x1 = [["x1", "y1"], ["x1", "x2"]]
x2 = [["x1", "y1"], ["x1", "x2"], ["x2", "y2"]]
set1 = {tuple(item) for item in x1}
print [item for item in x2 if tuple(item) not in set1]
# [['x2', 'y2']]

Just convert the first set of items to a list of tuples and then create a set with the tuples. Then, for every item in the next list of lists, convert that to a tuple and check if it is not there in the set.

python find difference between two lists

You can convert the lists to sets and run the usual set operations such as difference or symmetric difference. For example, set(b) - set(a) evaluates to set([7, 8, 9]).

Python - Differences between two lists

Trying out your code, it seemed to work fine with me regardless of the length of the lists - when I added 100 to list1, it showed up for both difference functions.

However, there appear to be a few issues with your code that could be causing the problems. Firstly, you accept arguments list1 and list2 for both functions, but these variables are the same name as your list variables. This seems not to cause an issue, but it means that the global variables are no longer accessible, and it is generally a better practice to avoid confusion by using different names for global variables and variables within functions.

Additionally, your function does not take the symmetric difference - it only loops over the variables in the first list, so unique variables in the second list will not be counted. To fix this easily, you could add a line combining your lists into a sum list, then looping over that entire list and checking if each value is in only one of the lists - this would use ^ to do an xor comparison of whether or not the variable is in the two lists, so that it returns true if it is in only one of the lists. This could be done like so:

def difference_extra_credit(l1,l2):
list = l1 + l2
return [value for value in list if (value in l1) ^ (value in l2)]

Testing this function on my own has resulted in the list [4, 11, 7, 1], and [4, 11, 100, 7, 1] if 100 is added to list1 or list2.

Haskell function to check differences between two lists

Not sure if it's a typo, but you're not passing xs and ys to your function.

nearlyEqual d xs ys = foldr(&&) True $ zipWith (\x y -> abs(x-y)<=d)

should be

nearlyEqual d xs ys = foldr(&&) True $ zipWith (\x y -> abs(x-y)<=d) xs ys

at least for it to typecheck.

A clearer implementation would make use of all, which is of type Foldable t => (a -> Bool) -> t a -> Bool, and of the function composition operator (.):

nearlyEqual d xs ys = all ((<= d) . abs) $ zipWith (-) xs ys

where zipWith (-) xs ys is element-wise difference of the two lists, and all verifies that the predicate (<= d) . abs holds for all of the elements of that list; the predicate, given an argument, applies abs to it, and then (<= d) to the result.

How to compare two lists and copy the differences into a third (python)?

First of all, thanks to all those who helped.
For anyone trying to find a simpler (though not efficient) way of doing it:

def wrong_input(input_list, mc_answers): # The two parameters are lists from main() and user_input()

wrong_answers=[]
for i in range(5):
if input_list[i]!=mc_answers[i]:
wrong_answers.append(input_list[i])

return wrong_answers

I changed the while to an if statement after @Barmar pointed out that a while loop will never end in this case.

Unrelated points to keep in mind (in case anyone comes across them, as I have):

  • A greater range than the lists you're comparing (i.e., range(6) instead of range(5) in this code.) will result in the error list index out of range (Both lists in this code have a length of 5.)
  • The first version of my code resulted in the error function 'object' not subscriptable. @Barmar pointed out that I left out the parenthesis when referring to the function user_input input_list=user_input instead of input_list=user_input(), which caused this error.

How to find difference between two lists?


List<int> list1 = [1,2,3,4,5];
List<int> list2 = [1,2,3,4,5,6,7];
List<int> filtered = list2.where((i) => !list1.contains(i)).toList();


Related Topics



Leave a reply



Submit