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]
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.
list(Tup[0])
Extract the numpy array from the tuple and convert it into a list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With