Luca
Luca

Reputation: 10996

python: replace elements in list with conditional

I am trying to do the following with python and am having a strange behavior. Say I have the following list:

x = [5, 4, 3, 2, 1]

Now, I am doing something like:

x[x >= 3] = 3

This gives:

x = [5, 3, 3, 2, 1]

Why does only the second element get changed? I was expecting:

[3, 3, 3, 2, 1]

Upvotes: 9

Views: 27733

Answers (3)

Kasravnd
Kasravnd

Reputation: 107287

Because Python will evaluated the x>=3 as True and since True is equal to 1 so the second element of x will be converted to 3.

For such purpose you need to use a list comprehension :

>>> [3 if i >=3 else i for i in x]
[3, 3, 3, 2, 1]

And if you want to know that why x >= 3 evaluates as True, see the following documentation :

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

In python-2.x and CPython implementation of course, a list is always greater than an integer type.As a string is greater than a list :

>>> ''>[]
True

In Python-3.X, however, you can't compare unorderable types together and you'll get a TypeError in result.

In [17]: '' > []
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-052e7eb2f6e9> in <module>()
----> 1 '' > []

TypeError: unorderable types: str() > list()

Upvotes: 19

dawg
dawg

Reputation: 103744

You can use this syntax with Numpy:

>>> import numpy as np
>>> x = np.array([5, 4, 3, 2, 1])
>>> x[x>3]=3
>>> x
array([3, 3, 3, 2, 1])

You can also do this with Pandas:

>>> import pandas as pd
>>> x = pd.Series([5, 4, 3, 2, 1])
>>> x
0    5
1    4
2    3
3    2
4    1
dtype: int64
>>> x[x>3]=3
>>> x
0    3
1    3
2    3
3    2
4    1
dtype: int64

Upvotes: 6

mgilson
mgilson

Reputation: 309841

You're using python lists. In python(2.x), comparison of a list with an int will compare the types, not the values. So, your comparison results in True which is equivalent to 1. In other words, your expression is equivalent to:

x[1] = 3  # x[1] == x[True] == x[x > 3]

Note, python3.x disallows this type of comparison (because it's almost certainly not what you meant) -- And if you want to be doing this sort of operation, you almost certainly thought of it by looking at numpy documentation as the numpy API has been designed specifically to support this sort of thing:

import numpy as np
array = np.arange(5)
array[array > 3] = 3

Upvotes: 5

Related Questions