Python List - Only Keep Only-Positive or Only-Negative Values

python list - only keep only-positive or only-negative values

Using a list comprehension.

Ex:

l = [1, 2, 3, -1, -2, -5, 6, 7, -6]
print([i for i in set(l) if -i not in l])

Output:

[3, -5, 7]

How to sort only positive numbers from a list?

You have several issues:

  • you have a variable that's named the same as a basic type (list), which shadows the type; pick a better name
  • for i in range(my_list): doesn't do what you think; you could do for i in range(len(my_list)):, but you can also just for n in my_list: and n will be every element in my_list in turn
  • your user enters text, you'll need to turn those strings into integers before comparing them to other integers, using int()
  • you do for i .. but also i += 1 you don't need to increment the for-loop variable yourself.

Look into list comprehensions, they are perfect for what you're trying to do in a more complicated way, to construct positive_list.

Your solution could be as simple as:

print(sorted([int(x) for x in input().split() if int(x) > 0]))

But staying closer to what you were trying:

numbers = [int(x) for x in input().split()]
sorted_positive_numbers = sorted([x for x in numbers if x > 0])
print(sorted_positive_numbers)

If you insist on a for-loop instead of a list comprehension:

numbers = [int(x) for x in input().split()]
positive_numbers = []
for number in numbers:
if number > 0:
positive_numbers.append(number)
print(sorted(positive_numbers))

How to select rows with only positive or negative values in Pandas

You can use the OR (|) to combine conditions:

print(df[(df[df.columns] >= 0).all(axis=1) | (df[df.columns] <= 0).all(axis=1)])

Prints:

   x  y  z
1 2 3 1
2 3 2 5
5 -3 -2 -1

Or simpler:

print(df[(df >= 0).all(axis=1) | (df <= 0).all(axis=1)])

EDIT: As @Erfan stated in the comments, if you want strictly negative (without 0), you use <:

print(df[(df >= 0).all(axis=1) | (df < 0).all(axis=1)])

Extract positive and negative values from an array in Python

The solution

Use two np.wheres to replace the values below zero and above zero:

>>> import numpy as np
>>> A = np.array([[-1, 2, -3], [4, -5, 6], [-7, 8, -9]])
>>> B = np.where(A > 0, A, 0)
>>> C = np.where(A < 0, A, 0)
>>> B
array([[0, 2, 0],
[4, 0, 6],
[0, 8, 0]])
>>> C
array([[-1, 0, -3],
[ 0, -5, 0],
[-7, 0, -9]])

The explanation

The three argument form of np.where broadcasts the arguments, the first and second already have the same shape (3, 3) but the 0 will be broadcasted to:

>>> Ag0_bc, B_bc, zero_bc = np.broadcast_arrays(A > 0, A, 0)
>>> Ag0_bc
array([[False, True, False],
[ True, False, True],
[False, True, False]], dtype=bool)
>>> zero_bc
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])

Then np.where will create a new array and fill each element in the new array with the corresponding element from the second argument (A) if the element in the first argument (A > 0) is True and take the element from the third argument 0 in case it's False.



Related Topics



Leave a reply



Submit