-1

I have two lists in python.Now I want to write a code which will search for the elements in the two lists.If they have the same element it return true else false. Repetitions are allowed and the order could be different.

Note: I do not want to use the "set" in-build function(Because it makes it so easy) or I don't want to do something like this: for item1, item2 in zip(list1, list2): ........... or if(list1 == list2): return True Because this can be done only when there are same number of elements and order is same

learner
  • 19
  • 4
  • So you want to know if there is at least one element that is in both lists? – Alex Hall May 21 '16 at 12:53
  • 2
    This question is very similar to [this question](http://stackoverflow.com/q/37361817/4014959)... – PM 2Ring May 21 '16 at 13:00
  • @DonkeyKong I am not getting the answer after using the answer you pointed. alets take input in a list1 = [1,2,3] and list2 = [3,2,2,1]. Now this should give me answer as "matched" – learner May 21 '16 at 13:15

2 Answers2

0

The sensible way to do this is to use sets. But if you insist on doing it inefficiently by looping, then you need to check that each element of the first list is an element of the second list, and vice versa.

Here are 3 ways to do that check. Notice that (unlike the code in your earlier question) I'm directly iterating over the list elements; there is no need to use list indices for this task in Python.

Firstly, we use an explicit flag ok to keep track of matches.

#Test if each element of `b` is also an element of `a`
def contains(a, b):
    for v in b:
        ok = False
        for u in a:
            if v == u:
                ok = True
                break
        if not ok:
            return False
    return True

This can be condensed by attaching an else clause to the inner for loop:

def contains(a, b):
    for v in b:
        for u in a:
            if v == u:
                break
        else:
            return False
    return True

But we can make it even shorter by using the built-in any function and a generator expression:

def contains(a, b):
    for v in b:
        if not any(v == u for u in a):
            return False
    return True

Note that any(v == u for u in a) will stop testing as soon as it finds a u that matches v.

Now for the full matching test. This code works on Python 2 or Python 3.

from __future__ import print_function

def contains(a, b):
    for v in b:
        if not any(v == u for u in a):
            return False
    return True

def match(a, b):
    return contains(a, b) and contains(b, a)

target = [1,2,3,4,5]
data = ([3,5,1,2,4], [1,2,3,4,5,6], [1,2,3,4,6], [3,1,2,1,4])

print(target)
for seq in data:
    print(seq, contains(target, seq), contains(seq, target), match(target, seq))

output

[1, 2, 3, 4, 5]
[3, 5, 1, 2, 4] True True True
[1, 2, 3, 4, 5, 6] False True False
[1, 2, 3, 4, 6] False False False
[3, 1, 2, 1, 4] True False False
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
0

I would use something like this:

def list_check(a, b):
    for item in a:
        if item in b:
            return True
    return False

Or if you were looking for something more complex, but shorter, I would go with this:

check = lambda a,b: {'answer': True if True in [True if x in b else False for x in a] else False}['answer']

result:
l_1 = [1, 2]
l_2 = [1, 2, 3, 4]
print check(l_1, l_2)
True

l_1 = ['c', 3 , 9]
l_2 = ['cat', 4, '9']
print check(l_1, l_2)
False

Note: Sorry for all the edits, there was a logic flaw in the second method that would cause true to only be returned if the last element of a was present in b. This will work for both.

JPope2014
  • 161
  • 1
  • 10