I need to initialize multiple numpy arrays with the same shape. Wondering which way is the best to go:
- write a line for each:
dist_x=np.zeros((1,len(pose07)-1))
dist_y=np.zeros((1,len(pose07)-1))
rots_absulute=np.zeros((1,len(pose07)-1))
rots=np.zeros((1,len(pose07)-1))
- calculate the length first and save it as a parameter:
length=len(pose07)-1
dist_x=np.zeros(1,length)
dist_y=np.zeros(1,length)
rots_absulute=np.zeros(1,length)
rots=np.zeros(1,length)
- initialize one of them, then copy it multiple times:
dist_x=np.zeros((1,len(pose07)-1))
dist_y=np.copy(dist_x)
rots_absulute=np.copy(dist_x)
rots=np.copy(dist_x)
or maybe there is a better way?

np.empty_likeornp.zeros_like. At least it looks cleaner. If you know for sure that you're going to overwrite all elements,np.emptycan be faster thannp.zeros, depending on whether it's memory allocated via the operating system or re-used memory that was already allocated by the process.(1,length)as opposed to just(length,)?