0

So I used operator.attrgetter to try to rip an array class into a numpy array by tupleList=map(attrgetter(*inNames),inClass) and ended up with something like this for 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]) and np.asAnyArray(tupleList[0]) don't work and I get

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?

1 Answer 1

2

Use np.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.])
Sign up to request clarification or add additional context in comments.

3 Comments

That works 1d, is there a way to do this for the whole tupleList?
@DanielForsman But your question relates to 1D and not the entire tupleList. Changing the requirements of your question after an answer has been provided is not very fair.
Understood. In any case it seems np.array([np.hstack(t) for t in tupleList]) works.

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.