Prevent numpy from creating a multidimensional array

前端 未结 4 987
误落风尘
误落风尘 2020-11-29 11:23

NumPy is really helpful when creating arrays. If the first argument for numpy.array has a __getitem__ and __len__ method these are use

4条回答
  •  萌比男神i
    2020-11-29 11:54

    This workaround may not be the most efficient, but I like it for its clarity:

    test_list = [Test([1,2,3]), Test([3,2,1])]
    test_list.append(None)
    test_array = np.array(test_list, dtype=object)[:-1]
    

    Summary: You take your list, append None, then convert to a numpy array, preventing numpy from converting to a multidimensional array. Finally you just remove the last entry to get the structure you want.

提交回复
热议问题