I try to create a multidimensional array in python 3.7. Unfortunately I do not know the variable type, the dimensions or the amount of dimensions beforehand. My approach so far is a loop:
dimensions = [3,3]
vartype = 'binary'
if vartype=='binary':
new_array=False
elif vartype=='int':
new_array=0
for dim in dimensions:
new_array = [new_array for _ in range(dim)]
The issue is, that if there is more than 1 dimension I end up with multiple references to the first dimension:
new_array[0][1]=True
print(new_array)
[[False, True, False],
[False, True, False],
[False, True, False]]
This has been addressed quite some times like here. In other cases however the amount of dimensions is known, so I can't transfer the solution to my problem. Does anyone know a way to create arrays of varying amount of dimensions?