Python function will not output indexed arrays

Question:

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.

Asked By: I hate coding

||

Answers:

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
Answered By: Chinny84

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

Answered By: Rahul K P
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.