0
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.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
ronald
  • 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 Nov 10 '16 at 16:54
  • 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 Nov 10 '16 at 22:01

1 Answers1

0

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();
}
sahoora
  • 245
  • 1
  • 5
  • thanks for your answer but List matchedElement = new ArrayList(); SyntaxError: invalid syntax. and i wrote newArrayList it is OK. but for (ClassA a: list1) {SyntaxError: invalid syntax. i could not fix and i am new with python and these codes seems very difficult for me. i have no very information to understand these codes. – ronald Nov 10 '16 at 22:22
  • For Python you may need to see another thread in stackoverflow. – sahoora Nov 11 '16 at 10:54
  • [link]http://stackoverflow.com/questions/16138015/checking-if-all-the-elements-in-one-list-are-also-in-another – sahoora Nov 11 '16 at 10:54