#### Category : arrays

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 ..

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))) 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 ..