Search code examples
pythonarraysnumpytypespre-allocation

Initialize 64 by 64 numpy of (0,0) tuples in python?


Is it possible to create a numpy of any arbitrary data structure, for example tuples? If yes, how do I initialize it without writing it out? (Obviously, I don't want to write out 64 by 64 array)


Solution

  • Another way:

    value = np.empty((), dtype=object)
    value[()] = (0, 0)
    a = np.full((64, 64), value, dtype=object)
    

    Some trickery is required here to ensure that numpy does not try to iterate the tuple, hence the initial wrapping in an object array