I need to create an array with the same data from another array but just the positive values.
I was trying this code below:
if a > 0:
arr = a
I need to create an array with the same data from another array but just the positive values.
I was trying this code below:
if a > 0:
arr = a
Here two possible options:
Option 1: Initialize an empty list that is going to contain the array with positive numbers:
array1 = [1, 2, -1, -3, 5, 6, -9]
array2 = []
for item in array1:
if item > 0:
array2.append(item)
Option 2:
Use a list comprehension to create the second array:
array1 = [1, 2, -1, -3, 5, 6, -9]
array2 = [item for item in array1 if item > 0]
In both cases the output will be:
print(array2)
==> [1, 2, 5, 6]