0
votes
import Counter
import collections
import itertools, collections

List1=[('1234', '5678', 9101112, 131415, 161716, 19), ('1234', '5678', 9101112, 131415, 161716, 19), ('1723113685', '1958120268', 704338729, 1310973186, 38760, 80), ('1723113685', '1958120268', 704338729, 1310973186, 38760, 70), ('1234', '5678', 9101112, 131415, 161716, 19), ('19216813', '6311624397', 3851697578, 0, 58816, 80), ('6311624397', '19216813', 2747564191, 3851697579, 890, 58816), ('19216813', '6311624397', 3851697579, 2747564192, 58816, 80), ('1234', '5678', 9101112, 131415, 161716, 19), ('19216813', '6311624397', 3851698039, 2747565640, 58816, 50)]

List2=[('1723113685', '1958120268', 704338729, 1310984130, 38760, 80), ('1723113685', '1958120268', 704338729, 1310985498, 38760, 80), ('1723113685', '1958120268', 704338729, 1310986866, 38760, 80), ('1723113685', '1958120268', 704338729, 1310973186, 38760, 80), ('6311624397', '19216813', 2747564192, 3851697579, 80, 58816), ('19216813', '6311624397', 3851698039, 2747564192, 58816, 80), ('19216813', '6311624397', 3851698039, 2747565640, 58816, 80), ('1234', '5678', 9101112, 131415, 161716, 19), ('19216813', '6311624397', 3851698039, 2747568536, 58816, 80), ('19216813', '6311624397', 3851698039, 2747569984, 58816, 80), ('19216813', '6311624397', 3851698039, 2747571432, 58816, 80), ('19216813', '6311624397', 3851698039, 2747572880, 58816, 80)]


ab=[]
abDict = Counter(ab) 
for x in List1:
    if x in List2:

    ab.append(x)

for key, value in abDict.items():
    if value>2:
    print key 

I want to find all matched elements between list1 and list2. When I change list1 and list2 with counter, it is not find all element, but only shows match one element.

This could displays all elements, but I can't find how many elements.

1
Is there another part of your code that shows how you're doing this: "when i change list1 and list2 with counter"? If not, what is the expected output and what is the output you're getting? - nvioli
there is no another part because i cant take result with it. when i change them counter or if not, output is match element but display only one times actually there are many same elements. if list1 have 1,1,1 and if list2 have 1 then i want 1,1,1.. i take 1,1,1 but if value >2 not works - ronald

1 Answers

0
votes

Go through list1 and check if element contains in list2 and override equals/hashcode method in the class which the list holds.

List<ClassA> list1 = [a1, a2];
List<ClassA> list2 = [a3, a4];

List<ClassA> matchedElement = new ArrayList<ClassA>();
for (ClassA a: list1) {
   if (list2.contains(a)){
     matchedElement.add(a); 
   }
}

System.out.println(matchedElement);

public Class ClassA {
@Override
    public boolean equals(Object obj) {
//compare objects values from A
}

@Override
    public int hashCode() {
final int prime = 31;
int result = 1;
        result = prime
                * result + obj.hashCode();
}