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?
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])
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]
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