Overlapping lists function returns False when True in python

柔情痞子 提交于 2020-01-06 04:21:07

问题


I'm a programming semi-noob and am working through Torbjoern Lager's 46 Simple Python Exercises. This is number 10: Define a function overlapping() that takes two lists and returns True if they have at least one member in common, False otherwise. You may use your is_member() function, or the in operator, but for the sake of the exercise, you should (also) write it using two nested for-loops.

def over(list1,list2):
    for i in list1:
        for j in list2:
            return i==j

I thought I had a nice, simple solution, but it can't recognize that the lists overlap, unless the overlapping elements are the first ones.

over(["a","b","c","d"],["e","f","a","h"]) 

returns False

over(["a","b","c","d"],["a","f","g","h"])

returns True

For some reason, it's not searching through all of the combinations. Any help would be appreciated.


回答1:


It's not searching through all the combinations because you're returning on the first iteration of the nested loop. You could do this:

def over(list1,list2):
    for i in list1:
        for j in list2:
            if i == j:
                return True

    return False

This returns True as soon as any overlap is found. If no overlap is ever found, it'll get to the last line and return False.




回答2:


The problem is that you return i==j on the first iteration. Your function will justs compare list1[0] and list2[0]. The solution is to add if. Here is an example:

def over(list1,list2):
    for i in list1:
        for j in list2:
            if i == j:
                return True
    return False



回答3:


You should be testing with an if as suggested in the other answers as you are returning after the very first iteration but using any would be a nicer approach:

def over(list1,list2):
    return any(i ==j for i in list1 for j in list2)

Which is equivalent to:

def over(list1,list2):
    for i in list1:
        for j in list2:
            if i == j:
                return True
    return False

short circuiting on a match and returning True if there is any match or returning False if there are none.

Or using sets for larger input would be the fastest approach:

def over(list1, list2):
    return not set(list1).isdisjoint(list2)

if not set(list1).isdisjoint(list2) is True we have at least one common element.




回答4:


When you execute the "return" stament de execution stops there, like it happens in Java. It returns true because you have 'a' in the first position in both arrays.

You can try this:

result = False;
for i in list1:
         for j in list2:
            if i == j:
                result=True;
return result

If you want it more efficient:

for i in list1:
     for j in list2:
        if i == j:
            return True;
return False;


来源:https://stackoverflow.com/questions/32029403/overlapping-lists-function-returns-false-when-true-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!