简体   繁体   中英

how to change a array of a single column to a row of values instead of arrays in python

I am trying to convert an array of arrays that each contain only one integer to a single array with just the integers.

This is my code below. k=1 after the first for loop and the next code deletes all the rows of except the first one and then transposes it.

handles.Background = np.zeros(((len(imgY) * len(imgX)),len(imgZ)))
WhereIsBackground = np.zeros((len(imgY), len(imgX)))
k = 0
for i in range(len(imgY)):
    for j in range (len(imgX)):
        if img[i,j,handles.PS_Index] < (handles.PS_Mean_Intensity / 8):
            handles.Background[k,:] = img[i,j,:]
            WhereIsBackground[i,j] = 1
            k = k+1

handles.Background = np.delete(handles.Background,np.s_[k:(len(imgY)*len(imgX))+1],0).T

At this point, I can access data by using handles.Background[n] but this returns an array that contains a single integer. I was trying to convert the handles.Background so that when I do handles.Background[n] , it just returns a single integer instead of an array containing that value. So, I'm getting array([0.]) when I run handles.Background[0] , but I want to get just 0 when I run handles.Background[0]

I've observed that int(handles.Background[i]) returns an integer and tried to reassign them using a for loop but the result didn't really change. What would be the best option for me?

    for i in range (len(handles.Background)):
        handles.Background[i] = int(handles.Background[i])

if handles.Background[n] returns an array, you can index into that, too, using the same [n] notation.

So you are looking for

handles.Background[n][0]

If you want to unpack the whole array at once, you can use this:

handles.Background = [bg[0] for bg in handles.Background]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM