Compare Two Lists for Differences

Get difference between two lists with Unique Entries

To get elements which are in temp1 but not in temp2 (assuming uniqueness of the elements in each list):

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 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 can I compare two lists in python and return matches

Not the most efficient one, but by far the most obvious way to do it is:

>>> a = [1, 2, 3, 4, 5]
>>> b = [9, 8, 7, 6, 5]
>>> set(a) & set(b)
{5}

if order is significant you can do it with list comprehensions like this:

>>> [i for i, j in zip(a, b) if i == j]
[5]

(only works for equal-sized lists, which order-significance implies).

Compare two Lists and get the difference

If you're using apache common-collections, try using the below, otherwise there's plenty other options shared in Joe's link

List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(1, 4, 5);

Collection<Integer> list1WithoutList2 = CollectionUtils.removeAll(list1, list2);
System.out.println(list1WithoutList2); //Prints 2,3

Collection<Integer> list2WithoutList1 = CollectionUtils.removeAll(list2, list1);
System.out.println(list2WithoutList1); //Prints 4,5

System.out.println(Stream.concat(list1WithoutList2, list2WithoutList1).toSet()); //Prints 2,3,4,5

comparing two lists and finding indices of changes

for index, (first, second) in enumerate(zip(list1, list2)):
if first != second:
print(index, second)

Output:

1 K
6 T

If you want the output you gave, we need to count from 1 instead of the usual 0:

for index, (first, second) in enumerate(zip(list1, list2), start=1):

compare two lists in tcl and obtain list of only differences between lists

In general, each list could have elements that the other does not. The best approach is therefore to compute two result lists at the same time, being the elements present in one list that are absent in the other and vice versa. Then it's up to you to decide what to do with the information.

proc ComputeUniques {listA listB} {
set A {}
set B {}
foreach a $listA {
dict set A $a 0; # The 0 is just an arbitrary value
}
foreach b $listB {
dict set B $b 0
dict unset A $b
}
foreach a $listA {
dict unset B $a
}
return [list [dict keys $A] [dict keys $B]]
}

Note that dict unset does nothing if the key is absent, which is convenient, and the results are in the order of occurrence of the input keys (strictly in the order of first occurrence, in the case of input lists that are not set-like).

Compare two lists of different objects by certain two fields

It could make sense to merge id / title into a single String, remap the input lists into List<String> and then use AssertJ hasSameElements to compare the new lists:

assertThat(
aObjectList.stream()
.map(a -> String.join(":", a.aId, a.aTitle))
.collect(Collectors.toList())
).hasSameElementsAs(
bObjectList.stream()
.map(b -> String.join(":", b.bId, b.bTitle))
.collect(Collectors.toList())

);



Related Topics



Leave a reply



Submit