2

Before you mark this question as a duplicate, please read through the entire post...

I have a list of lists that looks something like...

>>> print(list_of_lists)
[[3, 3, 7, 8, 5], [9, 3, 3, 3, 3], [9, 10, 11, 3, 23, 3, 3], [20, 3, 3, 3, 3, 3, 3, 3], [20, 3, 3, 3, 3, 3, 3]]

I want to convert this list of lists into an array. However, when I do:

potential_numpy_array = numpy.array(list_of_lists)

or:

potential_numpy_array = numpy.asarray(list_of_lists)

I get something stranger:

>>> print(potential_numpy_array)
[list([3, 3, 17, 18, 16]) list([20, 3, 3, 3, 3]) list([20, 5, 6, 3, 12, 3, 3]) list([9, 3, 3, 3, 3, 3, 3, 3]) list([9, 3, 3, 3, 3, 3, 3])]

I've looked at many other questions but did not find an answer that could solve this problem.

Could someone please help me identify the source of confusion?

Thanks!

7
  • 4
    Don't use numpy with jagged lists, it will only cause you more of a headache later Commented Aug 14, 2018 at 14:30
  • Do you want it to be a numpy array with numpy arrays within it? Commented Aug 14, 2018 at 14:32
  • @user3483203 Okay, should I "pad" the lists? Commented Aug 14, 2018 at 14:32
  • 1
    Check here: stackoverflow.com/questions/10346336/… Commented Aug 14, 2018 at 14:32
  • @Hadus A list of numpy arrays is what I want (although a numpy array of numpy arrays would be fine too) Commented Aug 14, 2018 at 14:33

2 Answers 2

2

To create a list of numpy arrays:

np_arrays = []

for array in arrays:
    np_arrays.append(numpy.array(array))
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks to the help of fellow stackOverflow users, I realized the list was "jagged" and needed to be padded. An example of a jagged list is:

[ [1,2],
  [1,3,2],
  [1] ]

I needed:

[ [1,2,0],
  [1,3,2],
  [1,0,0] ]

Since I am using TensorFlow and keras, I can just do:

test_data = keras.preprocessing.sequence.pad_sequences(test_data,
                                                    value=word_index["<PAD>"],
                                                    padding='post',
                                                    maxlen=12)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.