I think I've found a way to do that with arrays of any dimension, and even if elements of the array don't all have the same dimension, for example:
array=[
[
['N',2],
[3,'N']
],
[
[5,'N'],
7
],
]
First, define a function to check if a variable is iterable, like the one from In Python, how do I determine if an object is iterable?:
def iterable(obj):
try:
iter(obj)
except Exception:
return False
else:
return True
Then, use the following recursive function to replace "N" with 0:
def remove_N(array):
"Identify if we are on the base case - scalar number or string"
scalar=not iterable(array) or type(array)==str #Note that strings are iterable.
"BASE CASE - replace "N" with 0 or keep the original value"
if scalar:
if array=='N':
y=0
else:
y=array
"RECURSIVE CASE - if we still have an array, run this function for each element of the array"
else:
y=[remove_N(i) for i in array]
"Return"
return y
Output for the example input:
print(array)
print(remove_N(array))
Yields:
[[['N', 2], [3, 'N']], [[5, 'N'], 7]]
[[[0, 2], [3, 0]], [[5, 0], 7]]
What do you think?