Python中两个数组的交集(Lambda表达式和过滤器函数)
在本文中,我们将借助Lambda表达式和过滤器功能来学习Python中两个数组的交集。
问题在于,给了我们两个数组,我们必须找出这两个数组中的共同元素。
算法
1. Declaring an intersection function with two arguments. 2. Now we use the lambda expression to create an inline function for selection of elements with the help of filter function checking that element is contained in both the list or not. 3. Finally, we convert all the common elements in the form of a list by the help of typecasting. 4. And then we display the output by the help of the print statement.
现在让我们看一下它的实现:
示例
def interSection(arr1,arr2): # finding common elements
# using filter method oto find identical values via lambda function
values = list(filter(lambda x: x in arr1, arr2))
print ("Intersection of arr1 & arr2 is: ",values)
# Driver program
if __name__ == "__main__":
arr1 = ['t','u','t','o','r','i','a','l']
arr2 = ['p','o','i','n','t']
interSection(arr1,arr2)输出结果
Intersection of arr1 & arr2 is: ['o', 'i', 't']
结论
在本文中,我们借助Lambda表达式和过滤器函数及其实现,了解了Python中两个数组的交集。
热门推荐
1 如何训练狗狗握手
10 新员工寄语祝福语简短
11 新婚敬酒祝福语大全简短
12 怎么训练狗狗
13 训练狗狗上厕所
14 春蚕老师祝福语简短励志
15 高考好运如愿祝福语简短
16 战哥过年祝福语简短
17 中考感谢朋友祝福语简短
18 长沙旅游祝福语大全简短