Home > Software engineering >  Combine an array(shape,) and a tuple (array (shape,)) of different shape and size
Combine an array(shape,) and a tuple (array (shape,)) of different shape and size

Time:02-25

Lets say I have an array with size 250,

arr.shape
(250,)

and an array with a size of 150, that's what I have from google colab, tuple: tup (1 item) (ndarray with shape (150,))

tup.shape
((150,))

Now I want to loop through all the values and combine these two based on the similarity if only arr[i] is equal to t in tup t[i].

combined_arr = []
for i in arr:
 for t in tup:
  if i == t[i]:
  #do this..
  #combine them into one arry 
  combined_arr.append(i)

How would I achieve this without out of range error?

That's the output of tup. I am not sure what is the type of this? how to get a single value from here and compare it with a single value of arr?

  (array([   1,    4,    5,   12,   16,   20,   21,   23,   26,   34,   36,
     39,   41,   60,   61,   63,   68,   74,   78,   84,   94,  107,
    108,  109,  113,  143,  153,  156,  168,  175,  176,  183,  186,
    187,  198,  214,  226,  243,  253,  268,  277,  292,  305,  306,
    307,  308,  315,  316,  320,  327,  341,  350,  363,  374,  380,
    385,  386,  390,  401,  403,  404,  405,  406,  414,  417,  418,
    420,  425,  426,  443,  467,  473,  474,  485,  486,  487,  489,
    507,  508,  514,  522,  523,  524,  525,  526,  540,  554,  582,
    587,  592,  595,  605,  629,  639,  651,  653,  659,  662,  664,
    665,  677,  685,  696,  720,  728,  732,  737,  740,  741,  745,
    748,  751,  753,  757,  759,  764,  768,  771,  774,  781,  785,
    790,  791,  798,  802,  809,  811,  814,  818,  843,  844,  850,
    851,  857,  880,  885,  898,  899,  901,  907,  952,  958,  966,
    978,  982,  983,  989,  997, 1011, 1036, 1038, 1054, 1061, 1080,
   1085, 1089, 1093, 1104, 1108, 1115, 1131, 1143, 1150, 1163, 1164,
   1198, 1202, 1212, 1217, 1220, 1236, 1248, 1253, 1264, 1271, 1304,
   1319, 1326, 1339, 1342, 1345, 1362, 1365, 1368, 1377, 1387, 1405]),)

CodePudding user response:

Take the ndarray out of the tuple and then compare:

arr2 = tup[0]
combined_arr = []

for i in arr:
  if i in arr2: combined_arr.append(i)

or with a list comprehension:

arr2 = tup[0]
combined_arr = [i for i in arr if i in arr2]

or numpy style with intersect1d() as @Barmar pointed out:

import numpy as np

...

arr2 = tup[0]
combined_arr = np.intersect1d(arr, arr2)

CodePudding user response:

This should work -

arr = [1,2,3,4,5,6,7,8,9,10]
tup = (32,5,4,6,90,45)

combined_arr = []
for i in arr:
    for t in tup:
        if i == t:
            #do this..
            #combine them into one arry 
            combined_arr.append(i)


print(combined_arr) # gives [4, 5, 6]

CodePudding user response:

In my opinion the most pythonic way would be using zip, to first zip the two sequences. This avoids any out of range errors. For example with a list comprehension this looks like that:

arr = [1, 2, 3, 4, 5, 60, 7]
tup = (10, 20, 30, 40, 50, 60, 7, 80, 90)
[arr_elem for arr_elem, tup_elem in (zip(arr, tup)) if arr_elem == tup_elem]

which will output the list

[60, 7]
  • Related