Abhishek Thakur - 1 year ago 180

Python Question

Is there a pythonic way to convert a structured array to vector?

For example:

I'm trying to convert an array like:

`[(9,), (1,), (1, 12), (9,), (8,)]`

to a vector like:

`[9,1,1,12,9,8]`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
In [15]: import numpy as np
In [16]: x = np.array([(9,), (1,), (1, 12), (9,), (8,)])
In [17]: np.concatenate(x)
Out[17]: array([ 9, 1, 1, 12, 9, 8])
```

Another option is `np.hstack(x)`

, but for this purpose, `np.concatenate`

is faster:

```
In [14]: x = [tuple(np.random.randint(10, size=np.random.randint(10))) for i in range(10**4)]
In [15]: %timeit np.hstack(x)
10 loops, best of 3: 40.5 ms per loop
In [16]: %timeit np.concatenate(x)
100 loops, best of 3: 13.6 ms per loop
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**