-4
Demand=[[12,-10,13,14,13,15,17,20,19,14],[-9,17,11,20,10,16,13,22,15,12]]

in the demand list I have to replace the negative values with a zero. How can I do this?

Epsi95
  • 8,832
  • 1
  • 16
  • 34

3 Answers3

3

You ca apply the max() function on the inner lists using list comprehensions:

output = [[max(0,n) for n in d] for d in Demand] 
Alain T.
  • 40,517
  • 4
  • 31
  • 51
2

You can try list comprehension of simple for loop with if else condition. If the value is >=0 then keep as it is, else, make it zero.

Demand=[[12,-10,13,14,13,15,17,20,19,14],[-9,17,11,20,10,16,13,22,15,12]]
output = [[j if j>=0 else 0 for j in i] for i in Demand]

print(output)
[[12, 0, 13, 14, 13, 15, 17, 20, 19, 14], [0, 17, 11, 20, 10, 16, 13, 22, 15, 12]]
Epsi95
  • 8,832
  • 1
  • 16
  • 34
0
for sublist in Demand:
    for i in range(len(sublist)):
        if sublist[i] < 0:
            sublist[i] = 0
        
J.Correa
  • 11
  • 4