Alex Azazel Alex Azazel - 17 days ago 5
Python Question

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)

Answer

Create an empty array of dtype=object:

a=np.empty((64,64), dtype=object)

then put tuples (or anything else) in it:

for y in range(64):
    for x in range(64):
        a[y,x] = (0,0)

The most import thing actually is the dtype=object, allowing you to put any Python object in it (losing the speed of vectorized operations however).