Given two arrays of the same length but different orders. How will you shuffle the two arrays in unison?
Shuffling two arrays in unison means reordering the elements of both arrays in the same pattern. Let’s understand this with the help of an example.
arr_1 = [[1, 1], [2, 2], [3, 3]]
arr_2 = [1, 2, 3]
Expected Output (After shuffling them in unison):
[3 1 2]
Thus, in this tutorial, you will learn different ways of solving the mission-critical question of shuffling two given arrays in unison. So, without further delay, let us dive into the solutions.
⚡Method 1: Using numpy.random.permutation
Approach: Call the
permutation() function of the numpy.random module and pass the length of the given arrays to this function. This returns a randomly permuted range of
len(array)-1. Let’s say that the result is stored in a variable
shuffler. Then, use the square bracket notation as:
- arr_1_shuffled = arr_1[shuffler]
- arr_2_shuffled = arr_2[shuffler]
This allows you to shuffle the two arrays based on the value of
import numpy as np # Given arr_1 = np.array([[1, 1], [2, 2], [3, 3]]) arr_2 = np.array([1, 2, 3]) # shuffling the two arrays in unison shuffler = np.random.permutation(len(arr_1)) arr_1_shuffled = arr_1[shuffler] arr_2_shuffled = arr_2[shuffler] print(arr_1_shuffled) print(arr_2_shuffled)
[[3 3] [1 1] [2 2]] [3 1 2]
NOTE: In layman’s terms, permutation means the arrangement of elements. So, [30, 20, 10] is a permutation of [10, 20, 30] and vice-versa. The
permutation() method of the numpy.random module returns a re-arranged array while keeping the original array unchanged. The above solution is a mere use case of this function.
⚡Method 2: Using sklearn.utils.shuffle()
Another way to approach the given problem is to use the
shuflle() method from the
sklearn.utils module. The shuffle method allows you to shuffle arrays and sparse matrices in a consistent way. You can pass the two arrays as
sklearn.utils.shuffle(arr_1, arr_2), which shuffles them consistently and then returns a shuffled copy of each array.
import sklearn import numpy as np # Given arr_1 = np.array([[1, 1], [2, 2], [3, 3]]) arr_2 = np.array([1, 2, 3]) # shuffling the two arrays in unison arr_1_shuffled, arr_2_shuffled = sklearn.utils.shuffle(arr_1, arr_2) print(arr_1_shuffled) print(arr_2_shuffled)
[[3 3] [2 2] [1 1]] [3 2 1]
⚡Method 3: Using numpy.random.shuffle
Another similar function that facilitates you with the ability to shuffle the given arrays is the shuffle method from the
numpy.random module. It takes a sequence as an input and shuffles the elements in it to return a rearranged version of the original sequence. Note that the shuffle method changes the original sequence itself.
- Create an array that contains elements within the range 0 to len(array) – 1 using
np.arange(len(arr_1)). In this case, the created array will be [0 1 2].
- Now shuffle the above array using the shuffle method, which re-arranges the elements of the array. Let’s say that after shuffling the above array, it looks as follows: [2 1 0]. This array can now be used as an index to shuffle the two arrays.
import numpy as np # Given arr_1 = np.array([[1, 1], [2, 2], [3, 3]]) arr_2 = np.array([1, 2, 3]) # shuffling the two arrays in unison shuffled = np.arange(len(arr_1)) np.random.shuffle(shuffled) print(arr_1[shuffled]) print(arr_2[shuffled])
[[3 3] [2 2] [1 1]] [3 2 1]
Before we wrap up here’s a coding challenge for you to sharpen your coding skills.
Given an array
nums consisting of 2n elements in the form [x1, x2,…,xn, y1, y2,…, yn].
Return the array in the form [x1, y1, x2, y2,…, xn, yn].
1 <= n <= 500
nums.length == 2n
1 <= nums[i] <= 10^3
Input: nums = [2, 5, 1, 3, 4, 7], n = 3
Output: [2, 3, 5, 4, 1, 7]
Explanation: Since x1 = 2, x2 = 5, x3 = 1, y1 = 3, y2 = 4, y3 = 7 so the answer is [2, 3, 5, 4, 1, 7].
Before you see the solution, it is highly recommended that you try to solve it yourself.
def shuffle_array(nums, n): shuffled =  for i in range(n): shuffled.append(nums[i]) shuffled.append(nums[i + n]) return shuffled nums = [2, 5, 1, 3, 4, 7] n = 3 print(shuffle_array(nums, n))
Well! If you want to dive deep into the problem, here’s the link that will guide you through a detailed list of solutions to the given coding challenge – [Google Interview] Shuffle the Array
In this tutorial, we have learned three different ways of shuffling two given arrays in unison. I hope this helped you. Please subscribe and stay tuned for more interesting tutorials and discussions. Happy coding! 🙂
Recommended: Finxter Computer Science Academy
- One of the most sought-after skills on Fiverr and Upwork is web scraping. Make no mistake: extracting data programmatically from websites is a critical life skill in today’s world that’s shaped by the web and remote work.
- So, do you want to master the art of web scraping using Python’s BeautifulSoup?
- If the answer is yes – this course will take you from beginner to expert in Web Scraping.