Python: Replace Elements In List With Conditional
Solution 1:
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 :
>>>[3if i >=3else 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 calllast)
<ipython-input-17-052e7eb2f6e9>in<module>()
----> 1 '' > []
TypeError: unorderable types: str() > list()
Solution 2:
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
Solution 3:
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
Post a Comment for "Python: Replace Elements In List With Conditional"