0

I'm trying to make a for loop that each time adds an array, to the end of an array of arrays and I can't quite put my finger on how to. The general idea of the program:

for x in range(0,longnumber):
    generatenewarray
    add new array to end of array

So for example, the output of:

newArray = [1,2,3]
array  = [[1,2,3,4],[1,4,3]]

would be: [[1,2,3,4],[1,4,3],[1,2,3]]

If the wording is poor let me know and I can try and edit it to be better!

0

2 Answers 2

1

Is this what you need?

list_of_arrays = []
for x in range(0,longnumber):
    a = generatenewarray
    list_of_arrays.append(a)
3
  • It's a numpy array and needs to return one, so apparend doesn't work I don't think.
    – Danny
    Commented Dec 8, 2015 at 23:50
  • well, your problem doesn't seem well-formed then, as your arrays have different dimensions, and the only way you could concatenate them would be to make a 1xN array, which I'm guessing is not what you want. If your individual arrays actually are the same size, then try numpy.vstack() mentioned above. big_array = numpy.vstack( (big_array, new_array))...note that growing arrays like this isn't exactly efficient...
    – gariepy
    Commented Dec 9, 2015 at 1:24
  • Better to append to a list. If it must be an array (object dtype), convert it at the end.
    – hpaulj
    Commented Dec 9, 2015 at 2:08
0

It's not pretty, but this will work. You turn both numpy arrays into lists, add those two lists, and finally convert the result into a new numpy array:

np.array(array.tolist() + newArray.tolist())

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.