1

Say I have about 640 numpy arrays to stack vertically. Each array is of the size (66, 1). Doing this manually like this:

A = np.vstack((Ne['State_1_inc'], Ne['State_2_inc'], Ne['State_3_inc'], Ne['State_4_inc'], ..., Ne['State_640_inc']))

would obviously take a long time and is very time consuming. The end result would have A the size (66,640). Anybody know if I could do a for loop that will pass in all of my 640 states so I can build my matrix? New to programming here, thanks!

vlaaaaaddd
  • 19
  • 5

1 Answers1

2

Assuming you want to use all the elements of your dictionary:

Ne = {1: [1,2,3], 2: [4,5,6]}
np.vstack(list(Ne.values()))
# array([[1, 2, 3],
#        [4, 5, 6]])

Else you can use a dict comprehension:

np.vstack([Nef[f'State_{i+1}_inc'] for i in range(640)])
mozway
  • 194,879
  • 13
  • 39
  • 75
  • This looks promising. But I'm still getting an error. in (.0) ----> 1 A = np.vstack(([Ne['State_{i+1}_complete'] for i in range(640)])) KeyError: 'State_{i+1}_complete' – vlaaaaaddd Nov 19 '21 at 05:01
  • Maybe because the variable `i` in the for loop is inside a string that causing some problems? – vlaaaaaddd Nov 19 '21 at 05:04
  • I forgot the `f` of the f-string, check the update – mozway Nov 19 '21 at 05:05