Daniel Forsman - 6 months ago 58

Python Question

So I used

`operator.attrgetter`

`tupleList=map(attrgetter(*inNames),inClass)`

`tupleList[0]`

`(1, array([0.0, 0.0, 0.0, 0.0], 'f'), 1)`

I'd like to turn that into a numpy array (specifically a structured array, but I can work that out) that looks like this

`array([1, 0.0, 0.0, 0.0, 0.0, 1], dtype=(('id', 'i8'), . . . .)])`

Unfortunately,

`np.asArray(tupleList[0])`

`np.asAnyArray(tupleList[0])`

`ValueError: setting an array element with a sequence`

Is there a way around this? Currently fighting my IT division to get pandas, but don't have it right now. I assume that would that help?

Answer

Use `no.hstack`

in order to integrate your tuple :

```
In [106]: foo = (1, np.array([0.0, 0.0, 0.0, 0.0], 'f'), 1)
In [107]: np.hstack(foo)
Out[107]: array([ 1., 0., 0., 0., 0., 1.])
```