简体   繁体   English

使用 Python 列表切片和递归查找列表交点

[英]Finding List Intersection using Python List Slices & recursion

def findIntersection(list1, list2):
    if list1 == [] or list2 == []:
        return []
    elif list1[0] < list2[0]:
        return [list1[0]] + findIntersection(list[1:], list2)
    elif list1[0] > list2[0]:
        return [list2[0]] + findIntersection(list1, list2[1:])
    else:
        return ([list1[0]] + findIntersection(list1[1:], list2[1:])) 

This is the code I have written so far, and the end goal is to find the intersection of two lists.这是我到目前为止编写的代码,最终目标是找到两个列表的交集。 So for instance, findIntersection([1,2,4], [0,2,3]) == [2] or findIntersection([0,2,3,5], [1,2,4,5,6]) == [2,5].例如, findIntersection([1,2,4], [0,2,3]) == [2] 或 findIntersection([0,2,3,5], [1,2,4,5,6 ]) == [2,5]。

Where should I start and what am I doing wrong.我应该从哪里开始,我做错了什么。 Also more explanation of python slices would be appreciated.另外,对 python 切片的更多解释将不胜感激。 Thanks.谢谢。

[i for i in list1 if i in list2]

Uses list comprehension.使用列表理解。 Cycles though each element in list1 and adds them to a new list if and only if they are found in list2.当且仅当在 list2 中找到它们时,循环遍历 list1 中的每个元素并将它们添加到新列表中。

If you want to use slicing and recursion, then you're very close... you only want to include an element in the intersection list if its in both lists - that's only true in the final else .如果你想使用切片和递归,那么你非常接近......你只想在交集列表中包含一个元素,如果它在两个列表中 - 这仅在最后的else Also you have a typo in your code - list where should be list1 .此外,您的代码list中有一个错字,其中应该是list1 Note also that this method requires the input lists to be sorted (either both ascending or both descending).另请注意,此方法要求对输入列表进行排序(升序或降序)。

def findIntersection(list1, list2):
    if list1 == [] or list2 == []:
        return []
    elif list1[0] < list2[0]:
        return findIntersection(list1[1:], list2)
    elif list1[0] > list2[0]:
        return findIntersection(list1, list2[1:])
    else:
        return [list1[0]] + findIntersection(list1[1:], list2[1:])

list1 = [0,2,3,5]
list2 = [1,2,3,4,5,6]
print(findIntersection(list1, list2))

Returns [2,3,5]返回[2,3,5]

Your problem is that you carefully return elements that are not in the intersection:你的问题是你小心地返回不在交集的元素:

elif list1[0] < list2[0]:
    return [list1[0]] + findIntersection(list[1:], list2)
elif list1[0] > list2[0]:
    return [list2[0]] + findIntersection(list1, list2[1:])
else:
    return ([list1[0]] + findIntersection(list1[1:], list2[1:]))

Of these three cases, the only one that identifies a valid intersection element, is the last one: the element is in both lists.在这三种情况中,唯一标识有效交集元素的是最后一种:该元素在两个列表中。

For the other two, your recursion is correct, but adding the lower element is an error.对于其他两个,您的递归是正确的,但添加较低的元素是错误的。 You have to discard this element, because you know it isn't in both lists.您必须丢弃此元素,因为您知道它不在两个列表中。

elif list1[0] < list2[0]:
    # Drop the lowest element of list1 and recur
    return findIntersection(list[1:], list2)
elif list1[0] > list2[0]:
    # Drop the lowest element of list2 and recur
    return findIntersection(list1, list2[1:])
else:
    The element is in both lists; add it to the solution and recur.
    return ([list1[0]] + findIntersection(list1[1:], list2[1:]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM