How do you intersect two arrays in python?

Home » Python » Python programs

Here, we are going to learn how to find the union and intersection of two arrays in Python programming language?
Submitted by Bipin Kumar, on October 25, 2019

Two arrays will be given by the user and we have to find the union and intersection of these arrays in the Python programming. To find the union and intersection of these arrays, we will use the bitwise or [|] and bitwise and [&] respectively between the set of the given arrays. Before going to solve this problem we will learn about the union and intersection.

Union and intersection of two arrays

A list that has the common distinct element from both arrays and if there are repetitions of the element then only one occurrence is considered, known as the union of both arrays.

A list that has common distinct elements from both arrays, is the intersection of both arrays.

Algorithm to solve this problem

  1. Initially, we will take two lists from the user which may have repeated numbers or not.
  2. We will take the bitwise or [|] between the sets of both arrays to find union and assign it into a variable A in the form of lists.
  3. To find the intersection of both arrays, we will use the bitwise and [&] between the sets of given arrays and assign it into a variable B in the form of lists.
  4. Print variable A and B which is our required output.

Let's start writing the Python program by the implementation of the above algorithm.

Code:

a=list[map[int,input['Enter elements of first list:'].split[]]]
b=list[map[int,input['Enter elements of second list:'].split[]]]

A=list[set[a]|set[b]]
B=list[set[a]&set[b]]

print['Union of the arrays:',A]
print['intersection of the arrays:',B]

Output

Enter elements of first list: 3 4 6 4  4 6 7 41
Enter elements of second list: 78 3 5 7 -1 9 2 -5
Union of the arrays: [2, 3, 4, 5, 6, 7, 41, 9, 78, -5, -1]
intersection of the arrays: [3, 7]

set[] function is inbuilt in Python which is used to convert a list into another list which does not contain duplicate or repeated elements.

Python Array Programs »


  1. HowTo
  2. Python NumPy Howtos
  3. NumPy Intersection of Two Arrays

Created: May-24, 2021

  1. NumPy Intersection With the numpy.in1d[] Method in Python
  2. NumPy Intersection With the numpy.intersect1d[] Method in Python

This tutorial will introduce the methods to perform intersection on NumPy arrays in Python.

NumPy Intersection With the numpy.in1d[] Method in Python

Intersection means the common elements in two sets of elements. If we want to find the intersection of two 1D NumPy arrays, we can use the numpy.in1d[] method in Python. The numpy.in1d[] method takes the two arrays, checks whether each element of the first array is present in the second array, and returns a boolean array that contains true for each element present in both arrays and false for each element present in the first array but not in the second array. We can use this resultant array as the first array index to get the common elements in both arrays.

import numpy as np

A = np.array[[2,3,5,7,11]]

B = np.array[[1,3,5,7,9]]

C = A[np.in1d[A, B]]
print[C]

Output:

[3 5 7]

We first created the two arrays with the np.array[] method. We then stored the intersection of both arrays inside array C with C = A[np.in1d[A, B]].

NumPy Intersection With the numpy.intersect1d[] Method in Python

We can also use the numpy.intersect1d[] method to find the intersection of two 1D arrays in Python. The numpy.intersect1d[] method takes the arrays and returns the sorted intersection in the form of another 1D array. See the following code example.

import numpy as np

A = np.array[[2,3,5,7,11]]

B = np.array[[1,3,5,7,9]]

C = np.intersect1d[A, B]
print[C]

Output:

[3 5 7]

We stored the intersection of arrays A and B inside array C with the numpy.intersect1d[] method in the above code.

Both methods work just fine, but the np.intersect1d[] method is easier to use than the np.in1d[] method.

How do you print the intersection of two arrays in Python?

To find the intersection of between two arrays, use the bitwise and [&] between the sets of given arrays and assign it into a variable Y in the form of lists. Print variable X and Y which is our required output.

How do you find the intersection of multiple arrays in Python?

Follow the below steps..
Convert two lists items into tuples using map..
Intersect two sets using intersection and map method..
Convert the result to list..
Print the result..

How do you intersect two lists in Python?

To perform the intersection of two lists in python, we just have to create an output list that should contain elements that are present in both the input lists. For instance, if we have list1=[1,2,3,4,5,6] and list2=[2,4,6,8,10,12] , the intersection of list1 and list2 will be [2,4,6] .

What is the intersection of two arrays?

The intersection is a list of common elements present in both arrays. The elements in the output can be in any order.

Bài mới nhất

Chủ Đề