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])
从元组中提取 numpy 数组并将其转换为列表
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!
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.