Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a tuple into a numpy array?

I have a tuple that contains a numpy array that I want to convert into just the numpy array. The current tuple is:

Tup = (array([ 7500, 10476, 10643, 13683, 14761]),)

i've tried using the np.asarray module but when I do this it just adds an array around the tuple instead of removing it as seen below:

Tup= np.asarray(Tup)
print(Tup)
Output: array([[ 7500, 10476, 10643, 13683, 14761]])

How would I convert Tup into just an array. My ideal output would be:

[7500, 10476, 10643, 13683, 14761]
like image 873
KGreen Avatar asked Nov 15 '25 22:11

KGreen


2 Answers

You seem to have an 1-tuple containing an array as the single element; just index with zero to get the first (zeroth) element to select the array:

arr = Tup[0]

To get to a bare Python list (as per your "ideal output"),

arr = list(Tup[0])

should do the trick.

like image 130
AKX Avatar answered Nov 18 '25 12:11

AKX


list(Tup[0])

Extract the numpy array from the tuple and convert it into a list

like image 29
Suraj Subramanian Avatar answered Nov 18 '25 10:11

Suraj Subramanian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!