Find the number of unique pairs in an array where one number can be created by swapping two digits of the other number. In the following array for example, there are three unique pairs. arr = [3,23,156,4324,324,651,165,32] (32,23), (156,165), and (651,156) output: 3 Note that 165 and 651 is not a pair because the problem ..

#### Category : arrays

I was trying to understand numpy masks better and decided to try a simple fizzbuzz exercise (since np arrays are homogenous, 9993 is "fizz", 9995 = "buzz", 9998 = "fizzbuzz"). However, I noticed behavior I cannot understand and was hoping that someone could explain. In the first case, I created my masks like that: In: ..

i cant figure out how to calculate the mean for the values of the individual columns, for the rows its easy enough. the code should work for any amount of columns and rows no matter if data is removed or added to the array. it should also work if the ranges of the rows are ..

I have 2 arrays: import numpy as np A = np.array([[np.nan,np.nan,0],[4,5,6],[7,8,9]]) B = np.zeros((len(A),len(A[0]))) For every NaN in A, I would like to replace the zero in B at the corresponding indices by np.nan. How can I do that ? I tried with masks but couldn’t make it work. B = np.ma.masked_where(np.isnan(A) == True, np.nan) ..

How do I find the difference between two 2D array in python ? First array and second array arr1 = [[1,1],[1,2],[1,3],[1,4],[1,5]] arr2 = [[1,2],[1,3],[1,4]] The result I want result = [[1,1],[1,5]] Source: Python..

I have a one-dimensional np.array which has the following pattern array = [seq_1, seq_2, seq_3] seq_1 and seq_3 are all 0 seq_2 contains at least a single 1 and I want to split array into seq_1, seq_2 and seq_3. For example, array = [0,0,0,1,1,1,1,0,0,0] seq_1, seq_2, seq_3 = [0,0,0], [1,1,1,1], [0,0,0] array = [0,1,0,0,0,0,0,0,1,0] seq_1, ..

I have a list of lists (of variable len) that needs to be converted into a numpy array. Example: import numpy as np sample_list = [["hello", "world"], ["foo"], ["alpha", "beta", "gamma"], []] sample_arr = np.asarray(sample_list) >>> sample_arr array([list([‘hello’, ‘world’]), list([‘foo’]), list([‘alpha’, ‘beta’, ‘gamma’]), list([])], dtype=object) >>> sample_arr.shape (4,) In the above example, I got a ..

I am trying to get the maximum value for each sub_array of an numpy array that was split. The sub_arrays have different sizes. Using np.max or np.amax, i.e. no cycles. I tried: sub_eta = np.split(eta, zero_down_crossings) sub_eta = np.array(sub_eta) max_values = np.max(sub_eta) I get this error ValueError: operands could not be broadcast together with shapes ..

Using pandas=1.1.5. I created a very large sparse matrix using Bag to Word. I want to convert the sparse matrix to array. But I get MemoryError: Unable to allocate 36.6 GiB for an array with shape (17799, 275656) and data type int64 I don’t have admin right to increase the memory in Advanced system settings. ..

I am trying to refactor procedural code of a numpy 3d array into it’s vectorized equivalent and the results are not matching . import numpy as np arr = np.empty((16,73,144)) boolArray = np.zeros((14,73,144)) for k in range(1,14): for j in range(0,73): for i in range(0,144): if (arr[k+1,j,i] == arr[k-1,j,i]): boolArray[k,j,i] = True The vectorized counterpart ..

## Recent Comments