Do before replace
print popdata[0][5], type(popdata[0][5])
To check your assumptions about it being list of floats.
You should have got this error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
[0.02,10.05,34,56,0.02,0.199999].replace(0.02,0)
AttributeError: 'list' object has no attribute 'replace'
How to replace float near some value:
>>> numberlist = [0.02,10.05,34,56,0.02,0.199999]
>>> new_numberlist = [ 0 if x==0.2 else x for x in numberlist ]
>>> print new_numberlist
[0.02, 10.050000000000001, 34, 56, 0.02, 0.19999900000000001]
>>> new_numberlist = [ 0 if abs(x-0.02)<1e-6 else x for x in numberlist ]
>>> print new_numberlist
[0, 10.050000000000001, 34, 56, 0, 0.19999900000000001]
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
EDIT:
To replace float with other number in list:
>>> numberlist = [0.02,10.05,34,56,0.02,0.199999]
>>> new_numberlist = [ 0 if x==0.02 else x for x in numberlist ]
>>> print(new_numberlist)
[0, 10.05, 34, 56, 0, 0.199999]
>>> ## more safe
>>> numberlist = [0.02,10.05,34,56,0.02,0.199999]
>>> new_numberlist = [ 0 if abs(x-0.02)<1e-6 else x for x in numberlist ]
>>> print(new_numberlist)
[0, 10.05, 34, 56, 0, 0.199999]
>>>
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
If you have exact limits I would have recommended staying with strings as much as possible as converting to float loose little accuracy so happens like this:
>>> a=0.33
>>> a
0.33000000000000002
See my second, safer alternative with abs() comparison for nearly same floats.
Unfortunately, as you need to do comparison with the different size numbers, maybe you need to change the numbers to floats. No came to my mind you can filter them like this:
numberlist = ['0.02','10.05','34','56','0.02','0.199999']
numberlist = [ '0' if float(x)<0.21 else x for x in numberlist]
print(numberlist)
Output:
['0', '10.05', '34', '56', '0', '0']
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852