Could someone please suggest a pythonic way to unwrap a numpy array with dtype=object?
For example, if I started with:
array([array([ 1, 2, 3]),
array([ 4, 5, 6]),
array([ 7])], dtype=object)
I would like to return:
array([ 1, 2, 3, 4, 5, 6, 7])
as quickly as possible. The order is important, and the actual numbers aren't just ascending integers.
The backstory is that the arrays are being pulled from a several-GB ASCII file of varying length and structure, and the data tables have a variable number of columns on each line and I just need to preserve the row-then-column order of the floats as they appear.
I'm also amenable to doing this with numpy.loadtxt if the functionality exists; I need to scan the file line-by-line and look for certain headers, then import an unknown number of columns and lines of data, and do this several times throughout the file.
Thanks for your time.
Assuming A as the input array, you can use np.concatenate to unwrap it, like so -
np.concatenate(A)
Sample run -
In [325]: A
Out[325]: array([array([1, 2, 3]), array([4, 5, 6]), array([7])], dtype=object)
In [326]: np.concatenate(A)
Out[326]: array([1, 2, 3, 4, 5, 6, 7])
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