Similar to this question: finding and replacing elements in a list (python) but with a multi-dimensional array. For example, I want to replace all the N’s with 0’s:
JavaScript
x
5
1
list =
2
[['N', 0.21],
3
[-1, 6.6],
4
['N', 34.68]]
5
I want to change it to:
JavaScript
1
5
1
list =
2
[[0, 0.21],
3
[-1, 6.6],
4
[0, 34.68]]
5
Advertisement
Answer
You can use a nested list comprehension:
JavaScript
1
3
1
l = [['N', 0.21], [-1, 6.6], ['N', 34.68]]
2
new_l = [[0 if b == 'N' else b for b in i] for i in l]
3
Output:
JavaScript
1
2
1
[[0, 0.21], [-1, 6.6], [0, 34.68]]
2