4

I am fairly new to Python. I looked at other similar topics but they are not answering exactly what I want to do. Here is the outcome:

coslist[1:4]
Out[94]: [array([[ 0.7984719]]), array([[ 0.33609957]]), 0]

This is what I want:

coslist=[0.7984719,0.33609957,0]

I tried this:

tolist=list(coslist)
tolist[1:3]
Out[98]: [array([[ 0.7984719]]), array([[ 0.33609957]])]

And this:

y=np.array(val).ravel().tolist()
y[1:4]
Out[99]: [array([[ 0.7984719]]), array([[ 0.33609957]]), 0]

As you see any of them is what I want. Any help will be appreciated.

Denis
  • 151
  • 1
  • 4
  • 11
  • 1
    Your `collist` is a list filled with 2D arrays and numeric values, which is pretty hard to work with. Are you sure that there is no way to construct this list where you have just the value stored in 2D array, and not the 2D array? If not perhaps just plain python loop where your extract the values is the way to go. – Akavall Jun 03 '15 at 18:18
  • You've screwed up your creation of this list. The proper solution to your problem will not just be some sort of post-processing step; you'll need to fix the code that creates it, or you'll just get more headaches down the line. – user2357112 Jun 03 '15 at 22:09

3 Answers3

1

There's a built-in method of numpy array.

coslist = [numpy.array([[ 0.7984719]]), numpy.array([[ 0.33609957]]), 0]
coslist = [x.tolist()[0][0] if type(x)==numpy.ndarray else x for x in coslist]

type(coslst) # Should print <type 'list'>
lst # Should print a list of lists
Aditya
  • 3,080
  • 24
  • 47
  • I am having an error like when I run it. It is probably because costliest is not a bumpy ndarray. It is a list of numpy ndarray. I am completely stuck – Denis Jun 03 '15 at 18:33
  • In that case post a small example where you put values in coslist. Keep the array small. – Aditya Jun 03 '15 at 18:35
  • This is the first three elements of the list: coslist[1:4] Out[94]: [array([[ 0.7984719]]), array([[ 0.33609957]]), 0] – Denis Jun 03 '15 at 18:39
  • Check coslist = [x.tolist()[0][0] if type(x)==numpy.ndarray else x for x in coslist] . Tell me if it works. May fail if the actual array is more complex than the example. – Aditya Jun 03 '15 at 18:45
1

First you need to transform your np.arrays in list, item by item:

coslist=[np.array(item).tolist() for item in coslist]

Then I think the only way to get rid of the list inside list inside list is through iteration:

aux=[]
for x in coslist:
    if type(x)==list:
        for k in range(len(x[0])):
        aux.append(x[0][k])
    else:
        aux.append(x)
coslist=aux

Notice that I've considered that each numpy array could have more than one value within. Cause if your list contains just np.arrays with one value each, you could just do:

coslist=[np.array(item).tolist() for item in coslist]
coslist=[x[0][0] if type(x)==list else x for x in coslist]

or, if you want a quick answer to your problem specifically, that will do:

coslist=[np.mean(coslist[k]) for k in range(len(coslist))]
mihasa
  • 967
  • 2
  • 10
  • 20
0

Multidimensional Array: example = np.array([[1,2,3],[1,2],[1,4,5,6,7]])

  • Using list comprehension result = [j_i for item in a for j_i in item]

  • use lambda and reduce import functools as ft result = ft.reduce(lambda x,y :x+y, a)

  • Using itertools import itertools chain = itertools.chain(*a) list(chain)

Reference: Flattening a shallow list in Python

Pramit
  • 1,373
  • 1
  • 18
  • 27