3 ms·
That does not zip though. In fact, it creates a 1-dimensional array of objects because the arrays do not have the same number of items. >>> a = [np.arange
by PartiallyTyped 5y ago
That does not zip though. In fact, it creates a 1-dimensional array of objects because the arrays do not have the same number of items.
>>> a = [np.arange(i) for i in range(1, 11)]
>>> np.array(a).T
array([array([0]), array([0, 1]), array([0, 1, 2]), array([0, 1, 2, 3]),
array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4, 5]),
array([0, 1, 2, 3, 4, 5, 6]), array([0, 1, 2, 3, 4, 5, 6, 7]),
array([0, 1, 2, 3, 4, 5, 6, 7, 8]),
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])], dtype=object)
In fact, if we are to zip them, it should yield:
>>> list(zip(*a))
[(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)]
zip_arrays otoh does give the same result.
>>> zip_arrays(a)
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
- johndough 5y agoYou are completely right. Nevertheless, when I zip over arrays of different length, it is usually a bug instead of being intentional. A code-golfed version for different array lengths: np.c_[[a[:min(map(len,arrays))].T for a in arrays]].T The trick to get a local variable within a list comprehension statement with for l in [foo()] from your zip_array function is very cool by the way, but I sacrificed it in favor of shorter code.
- PartiallyTyped 5y agoI like your your solution, I think using `a[0:min(map(len,arrays))]` (ie with 0) is faster because it returns a slice and not a copy of the array. I am glad you liked the trick!