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]

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

From: stackoverflow.com/q/36603042

Back to homepage or read more recommendations: