0

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)?

input = [[1,2,3,4],[4,5,-6,-7],[8,9,10,11]]
output= []
for i in input:
    if i < 0:
        value = 0
        output.append(value)
    else:
        value= i
        output.append(value)
maximus
  • 335
  • 2
  • 16

4 Answers4

1

You need a nested loop.

lists = [[1, 2, 3, 4], [4, 5, 6, 7], [8, 9, 10, 11]]
output = []
for l in lists:
    nested_list = []
    for i in l:
        if i < 0:
            nested_list.append(0)
        else:
            nested_list.append(i)

    output.append(nested_list)

Also, you shouldn't name your variable input as it will override the input() function.

1

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:

import numpy as np
inp = np.array([[-1,2,3,4],[4,5,-6,7],[8,9,-10,11]])
print (inp)
#[[ -1   2   3   4]
# [  4   5  -6   7]
# [  8   9 -10  11]]
inp[inp < 0] = 0 
print (inp)
# [[ 0  2  3  4]
# [ 4  5  0  7]
# [ 8  9  0 11]]
tiberius
  • 430
  • 2
  • 14
  • Numpy arrays are not a "much more efficient way of dealing with nested lists". They only apply specifically when each sublist is exactly the same size. They are a good way to write vectorized code but in specific conditions. For a problem that is as basic as this, using NumPy is seems to be an overkill. – Akshay Sehgal Jan 12 '22 at 17:11
  • That's a good point, I have clarified my post to mention that NumPy will only be better for matrices, not nested lists of arbitrary size. Given the elegance of this approach vs the wordiness of the non-numpy options given below, I disagree that NumPy is overkill. – tiberius Jan 13 '22 at 18:26
0

Here is my attempt:

l = [[1,2,3,4],[4,5,-6,-7],[8,9,10,11]]

res = [[x if x >= 0 else 0 for x in in_l] for in_l in l]
gil
  • 2,388
  • 1
  • 21
  • 29
0

You don't necessarily need to import any additional libraries for this, that would be an overkill. Base python provides all the necessary operations for handling nested data structures and conditional operations.

You can do this with a combination of list comprehension and ternary operators in python -

inp = [[1,2,3,4],[4,5,-6,-7],[8,9,10,11]]

[[0 if item<0 else item for item in sublist] for sublist in inp]

#|____________________|
#          |
#   ternary operator
[[1,2,3,4],[4,5,0,0],[8,9,10,11]]
  1. A list comprehension would allow you to avoid the list.append() and directly result in a list as output. Note, that you need a listed list comprehension since you want to iterate over elements of a list of lists.

  2. A ternary operator is a conditional expression of the form [on_true] if [expression] else [on_false]. This lets you add conditions (if-else) to the list comprehension on each element you are iterating.

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51