Skip to content
Advertisement

Apply condition in for loop to each element in a list of lists

I am trying to see if each value in array is less then 0 then output 0, else output the number itself. The output must have the same dimensions as the input. Currently the input is a 3 by 4 but output is a list. How do I get the output size the same (3 by 4 array)?

JavaScript

Advertisement

Answer

Python’s NumPy arrays are a much more efficient way of dealing with matrices, which are what you have if all of your nested lists are the same size. If I understood your question, your example can be simplified down to a single line:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement