1

This is what it looked like for me: I do not understand why it is not printing false

a = [1,2,3,4,5]
b=  [11,6,7,8,9]

def overlapping(a,b):
   bool= True 
   for x in range(len(a)):
     for y in range(len(b)):
          if b[y]==a[x]:
             check("True")
             break

def check(str):
      if str =="True":
           print "True"
      else:
           print "False"

overlapping(a,b)
Cœur
  • 37,241
  • 25
  • 195
  • 267
zozozo
  • 25
  • 1
  • 3
  • 3
    Because you only call `check` in the `True` case? And why are you passing it a *string*?! – jonrsharpe Oct 09 '15 at 07:44
  • Check out [Truth Value Testing](https://docs.python.org/2/library/stdtypes.html#truth-value-testing) in Python. It can be a common pitfall when you are getting started. – Steven Correia Oct 09 '15 at 07:52

4 Answers4

0

there is not need to loop twice: Do like this, Use set

a = [1,2,3,4,5]
b =  [11,6,7,8,9]
if set(a) & set(b):
    print "true"
else:
    print "False"

Make it more pythonic:

a = [1,2,3,4,5]
b = [11,6,7,8,9]
def overlapping(a, b):
    return bool(set(a) & set(b)) 
overlapping(a, b)
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • `len(set(a) & set(b)) > 0` is definitly _less_ pythonic than a mere `set(a) & set(b)` – bruno desthuilliers Oct 09 '15 at 07:55
  • @brunodesthuilliers can you explain me?? – Hackaholic Oct 09 '15 at 08:00
  • all Python objects have a truth value in a boolean context. In the case of builtin sequences or container types (dicts, sets, lists, tuples, strings etc), an empty object has a False value. So the pythonic idiom is `if container:`, not `if len(container) > 0`. If you really want to return a strictly boolean value it should be `return bool(set(a) & set(b))` – bruno desthuilliers Oct 09 '15 at 14:42
0

Using method any and generator expression

Code:

list1 = [1,2,3,4,5]
list2=  [11,6,7,8,9]

def overlapping(list1,list2):

    return any( value in list2 for value in list1)

print overlapping(list1,list2)

Making changes to your code:

a = [1,2,3,4,5]
b=  [11,6,7,8,9]

def overlapping(a,b):
    for x in a:
        if x in b:
            check("True")
            break
    else:
        check("False")


def check(strs):
    print strs

overlapping(a,b)

Notes:

There is no need to call function check since it prints the same value you could just print it

Community
  • 1
  • 1
The6thSense
  • 8,103
  • 8
  • 31
  • 65
  • 1
    `any` return true or false, you dont need to give if condition, only `return any( value in list2 for value in list1)` should work – Hackaholic Oct 09 '15 at 07:52
  • 1
    the list comprehension is useless here, you get the same result without the overhead of building list with a generator expression `any(value in list2 for value in list1)` – bruno desthuilliers Oct 09 '15 at 07:53
0
In [3]: a = [1,2,3,4,5]
   ...: b=  [11,6,7,8,9]
   ...: 
   ...: def overlapping(a,b):
   ...:     my_break = False
   ...:     for x in range(len(a)):
   ...:         if my_break: break
   ...:         for y in range(len(b)):
   ...:             if my_break: break
   ...:             if b[y]==a[x]:
   ...:                 check("True")
   ...:             else:
   ...:                 check("False")
   ...:                 my_break = True
   ...: 
   ...: def check(string):
   ...:     if string == "True":
   ...:        print "True"
   ...:     else:
   ...:        print "False"
   ...: 
   ...: overlapping(a,b)
   ...: 
False

or

In [4]: def overlapping(a, b):
   ...:     return any(x in b for x in a)
   ...: 

In [5]: overlapping(a, b)
Out[5]: False

Also don't use str word, its built-in type.

namit
  • 6,780
  • 4
  • 35
  • 41
0

Try this use return instead of break

a = [1,2,3,4,5]
b=  [33,6,7,8,9]

def overlapping(a,b):
    bool= True 
    for x in range(len(a)):
         for y in range(len(b)):
            if b[y]==a[x]:
                return(check("True"))

    return(check("False"))

def check(str):
      if str =="True":
           print "True"
      else:
           print "False"

overlapping(a,b)
Artier
  • 1,648
  • 2
  • 8
  • 22