NumPy: Partition an array in a specified position and move all the smaller elements to the left
NumPy Sorting and Searching: Exercise-7 with Solution
Write a NumPy program to partition a given array in a specified position and move all the smaller elements values to the left of the partition, and the remaining values to the right, in arbitrary order (based on random choice).
import numpy as np nums = np.array([70, 50, 20, 30, -11, 60, 50, 40]) print("Original array:") print(nums) print("\nAfter partitioning on 4 the position:") print(np.partition(nums, 4))
Original array: [ 70 50 20 30 -11 60 50 40] After partitioning on 4 the position: [-11 30 20 40 50 50 60 70]
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a NumPy program to sort a given complex array using the real part first, then the imaginary part.
Next: Write a NumPy program to sort the specified number of elements from beginning of a given array .
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Python: Find the number of occurrence of each values in an iterable
It returns True only if two values point to same object.
- 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