0

I am trying to convert a list into an array. The list is:

list = [[59.99, 58.648, 58.608, 62.944, 51.648, 49.397, 44.766, 40.066, 35.641, 33.825, 31.112, 28.644, 26.441, 24.592, 26.767], ... , [253.99, 7.8, 58.28, 744.4, 59.08, 10.37, 2.9, 33.356, 64.2, 4.5, 3.18, 24,77, 7.18, 92,5, 95.87]]

I have tried this :

A = numpy.array(list)

but when I type A, I get the following array:

array([list([59.99, 58.648, 58.608, 62.944, 51.648, 49.397, 44.766, 40.066, 35.641, 33.825, 31.112, 28.644, 26.441, 24.592, 26.767]), ..., list([253.99, 7.8, 58.28, 744.4, 59.08, 10.37, 2.9, 33.356, 64.2, 4.5, 3.18, 24,77, 7.18, 92,5, 95.87])], dtype=object)

How can I get ride of list() in the array ?

4
  • What do you see for set(map(len, L)) where L is your input list (don't shadow built-ins, i.e. call your input L not list)? If your sublists are not the same length, what you see is inevitable. Commented Aug 9, 2018 at 14:09
  • That is a 1d array containing lists. Given the mix of list sizes it can't create a 2d array. Commented Aug 9, 2018 at 14:15
  • yes my sublists are not the same length. Maybe I have to extract each list and zero-pad it so that I can convert the whole list to an array Commented Aug 9, 2018 at 14:16
  • It's just that I want to feed the whole thing to a neural net and I doesn't make any sense to say that a sample " s " has a feature " f " when it doesn't Commented Aug 9, 2018 at 14:19

4 Answers 4

1

You have sublists with mismatched lengths. As such, an array of lists is inevitable. As you suggested, you can zero-pad your sublists.

Here's one solution via a list comprehension:

L = [[0, 1], [2, 3, 4], [5, 6], [7]]

n = max(map(len, L))

res = np.array([i + [0]*(n-len(i)) for i in L])

print(res)

array([[0, 1, 0],
       [2, 3, 4],
       [5, 6, 0],
       [7, 0, 0]])
Sign up to request clarification or add additional context in comments.

1 Comment

well it's working. I am afraid I connot escape the zero padding. Thank you.
1

This should work

from itertools import chain

A = numpy.array(list(chain(*your_list)))

and don't name your variable list it is a reserved keyword

Comments

1

Your list does not contain lists of equal length, and that is the reason why numpy is unable to convert directly. For instance, if you try :

mylist = [[1,2,3], [2,3,4]]
l = np.asarray(mylist)

This will work as expencted, since lists contained in mylist are of the same length. Still, you can try doing it in two steps:

mylist = [[59.99, 58.648, 58.608, 62.944, 51.648, 49.397, 44.766, 40.066, 35.641, 33.825, 31.112, 28.644, 26.441, 24.592, 26.767],[253.99, 7.8, 58.28, 744.4, 59.08, 10.37, 2.9, 33.356, 64.2, 4.5, 3.18, 24,77, 7.18, 92,5, 95.87]]
list_of_arr = [np.array(x) for x in mylist]
arr = np.array(arr)

so as to have arrays instead of lists to deal with

Comments

0

First, don't use list as your variable name. list is a python reserved keyword. Use mylist or something else.

One thing you can do is create an empty list: compiledlist.

Then loop over the different arrays in mylist and extend them to compiledlist.

compiledlist = []
for sublist in mylist:
    compiledlist.extend(sublist)

A = np.array(compiledlist)

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.