Home > Back-end >  Python function will not output indexed arrays
Python function will not output indexed arrays

Time:10-13

I am experiencing the following issue in my project. I have created a reproducible example of my problem:

In[1]:  arr_in = np.array([1,2,3,4,5,6])

In[2]:  def central(imx):
            half = int(len((imx)/2))
            im_forth = imx[half:]
            im_back = imx[0:half:-1]
            return im_forth, im_back

In[3]:  xx, yy = central(arr_in)

In[4]:  xx
Out[4]: array([], dtype=int32)

In[5]:  yy
Out[5]: array([], dtype=int32)

I would like to know exactly why it is that I'm not receiving output, but rather just an empty array. Thanks in advance.

CodePudding user response:

your problem was

half = int(len((imx)/2))

this equates to

half = int(len( [1/2, 2/2, 3/2, 4/2, 5/2, 6/2]))

or

half = int(6) => 6

what you are trying to

half = int(len(imx)/2) => 3

or better still

half = len(imx)//2

CodePudding user response:

You little messed up calculating the half,

def central(imx):
    half = int(len(imx)/2) # Updated here
    im_forth = imx[half:]
    im_back = imx[0:half:-1]
    return im_forth, im_back

Expansion of int(len((imx)/2))

In [1]: (arr_in)/2
Out[1]: array([0.5, 1. , 1.5, 2. , 2.5, 3. ])

In [2]: len((arr_in)/2)
Out[2]: 6

In [3]: int(len((arr_in)/2))
Out[3]: 6

That's why it's returning the empty arrays

  • Related