4

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]
2
  • 2
    Just select the first element of the tuple Tup[0] Commented Mar 26, 2020 at 13:30
  • 1
    How about Tup[0]? Or do you actually want a list? Commented Mar 26, 2020 at 13:31

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1
list(Tup[0])

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

Comments

1

It's actually bit easier. What you need to do is simply use this code & it's done. array_from_tuple = np.array(tuple_name) where tuple_name is the name assigned to the object. For more features you can refer to this syntax:

numpy.array( object, dtype = None, *, copy = True, order = ‘K’, subok = False, ndmin = 0 )

Thanks for A2A! Keep exploring!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.