Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does x[x < 2] = 0 mean in Python?

I came across some code with a line similar to

x[x<2]=0 

Playing around with variations, I am still stuck on what this syntax does.

Examples:

>>> x = [1,2,3,4,5] >>> x[x<2] 1 >>> x[x<3] 1 >>> x[x>2] 2 >>> x[x<2]=0 >>> x [0, 2, 3, 4, 5] 
like image 329
aberger Avatar asked Apr 13 '16 15:04

aberger


People also ask

What does I 2 == 0 mean in Python?

0. if not i%2==0 can also be written as, if (i%2!=0) . i%2 gives the remainder obtained when i is divided by 2. So the expression stands true if the remainder of i when divided by 2 is not 0. Therefore, the expression stands true for all odd numbers because any odd number when divides with 2, leaves a remainder of 1.

What does x == 0 mean in Python?

== 0 means "equal to 0 (zero)". So if foo == 0: means "do the following if foo is equal to 0", thus if x % 2 == 0: means "do the following if x % 2 is equal to 0". Follow this answer to receive notifications.

What does i 3 == 0 mean in Python?

Answer 54d752ae86f5529472005430 It means that the remainder must be equal to zero when that number is divided by 3. == means “equal to”.

What does x2 mean in Python?

x^2 means "x to the power of 2", i.e. "x squared".


2 Answers

This only makes sense with NumPy arrays. The behavior with lists is useless, and specific to Python 2 (not Python 3). You may want to double-check if the original object was indeed a NumPy array (see further below) and not a list.

But in your code here, x is a simple list.

Since

x < 2 

is False i.e 0, therefore

x[x<2] is x[0]

x[0] gets changed.

Conversely, x[x>2] is x[True] or x[1]

So, x[1] gets changed.

Why does this happen?

The rules for comparison are:

  1. When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).

  2. When you order a numeric and a non-numeric type, the numeric type comes first.

  3. When you order two incompatible types where neither is numeric, they are ordered by the alphabetical order of their typenames:

So, we have the following order

numeric < list < string < tuple

See the accepted answer for How does Python compare string and int?.

If x is a NumPy array, then the syntax makes more sense because of boolean array indexing. In that case, x < 2 isn't a boolean at all; it's an array of booleans representing whether each element of x was less than 2. x[x < 2] = 0 then selects the elements of x that were less than 2 and sets those cells to 0. See Indexing.

>>> x = np.array([1., -1., -2., 3]) >>> x < 0 array([False,  True,  True, False], dtype=bool) >>> x[x < 0] += 20   # All elements < 0 get increased by 20 >>> x array([  1.,  19.,  18.,   3.]) # Only elements < 0 are affected 
like image 110
trans1st0r Avatar answered Sep 27 '22 02:09

trans1st0r


>>> x = [1,2,3,4,5] >>> x<2 False >>> x[False] 1 >>> x[True] 2 

The bool is simply converted to an integer. The index is either 0 or 1.

like image 28
Karoly Horvath Avatar answered Sep 25 '22 02:09

Karoly Horvath