NumPy: Find the set difference of two arrays
NumPy: Array Object Exercise-20 with Solution
Write a NumPy program to find the set difference of two arrays. The set difference will return the sorted, unique values in array1 that are not in array2.
import numpy as np array1 = np.array([0, 10, 20, 40, 60, 80]) print("Array1: ",array1) array2 = [10, 30, 40, 50, 70] print("Array2: ",array2) print("Unique values in array1 that are not in array2:") print(np.setdiff1d(array1, array2))
Array1: [ 0 10 20 40 60 80] Array2: [10, 30, 40, 50, 70] Unique values in array1 that are not in array2: [ 0 20 60 80]
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a NumPy program to get the unique elements of an array.
Next: Write a NumPy program to find the set exclusive-or of two arrays. Set exclusive-or will return the sorted, unique values that are in only one (not both) of the input arrays.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
>>> m = [x ** 2 for x in range(5)] >>> m [0, 1, 4, 9, 16]
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework