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?
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?
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]
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]]
for sublist in Demand:
for i in range(len(sublist)):
if sublist[i] < 0:
sublist[i] = 0