3

I have a numpy array x that looks like this:

In: x
Out: 
array([[array([ 1.]), 0.0],
       [array([-0.00770808]), array([ 0.90825723])],
       [array([-0.0358526]), array([ 0.59267366])],
       [array([ 0.0088844]), array([ 0.89480382])],
       [array([ 0.0387529]), array([ 0.56483939])],
       [array([-0.08508252]), array([ 0.20664826])],
       [array([-0.04159874]), array([ 0.538443])],
       [array([ 0.07626737]), array([ 0.25998565])],
       [array([ 0.00222671]), array([ 0.97386301])],
       [array([-0.12652962]), array([ 0.0621885])],
       [array([ 0.01404373]), array([ 0.83703121])]], dtype=object)

You see that there are 11 tuples. I want to create a list with each of the first elements from each tuples. That would be a list that looks like this: [1, -0.00770808, -0.0358526, 0.0088844, 0.0387529 ... ]. How can I do this?

2 Answers 2

3

You can do something like this:

>>> x[:,0].astype('float')
array([ 1.        , -0.00770808, -0.0358526 ,  0.0088844 ,  0.0387529 ,
       -0.08508252, -0.04159874,  0.07626737,  0.00222671, -0.12652962,
        0.01404373])
Sign up to request clarification or add additional context in comments.

2 Comments

Ohhh I see ... astype('float')...that's what I was missing... I just need the list as well so list(x[:,0].astype('float')) Thanks!
@Plug4 You can use x[:,0].astype('float').tolist() if you need a list.
1

You can simply issue

mylist = [a[0][0] for a in x]

where x is your array. Demo:

>>> mylist = [a[0][0] for a in x]
>>> mylist
[1.0, -0.00770808, -0.035852599999999998, 0.0088844000000000006, 0.0387529, -0.085082519999999995, -0.041598740000000002, 0.076267370000000001, 0.0022267099999999998, -0.12652962000000001, 0.014043730000000001]

Comments

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.