Hi,

I'm trying to replace an item in a list I made with 0.

popdata[0][5].replace(0.02,0)

when I run this it gives me the error TypeError: expected a character buffer object. I was wondering what this error meant and how I would go about fixing it. Thanks!

Elise

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]

Edited 6 Years Ago by pyTony: n/a

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]
>>>

I repro-d your error with this:

"".replace(0.02,0)

What exactly are you trying to do?

Edited 6 Years Ago by jcao219: n/a

Oh, my problem is that all my numbers where strings and not floats! I think the code above will work for me once I convert the numbers to floats. Thanks!

Hmm I can't seem to convert my strings to floats. I wrote a for loop to do it that looks like this

for i in range(int(P)):
    for j in range(int(A)):
        float([popdata[i][j]])

where P and A are the indicies of my array (its not really a list I guess, it's an array. I don't know if that changes things). When I do this I get an error that says the float argument must be a string or a number. Is there anything I should be doing differently?

Edited 6 Years Ago by echellwig: n/a

Try this. You had unneeded brackets.

for i in range(int(P)):
    for j in range(int(A)):
        float(popdata[i][j])

If the characters are floats they won't be in these '' in the array, correct? because mine still are. I'm not sure why.

You are not saving them, I think you are aware of it, but maybe not, so...

for i in range(int(P)):
    for j in range(int(A)):
        popdata[i][j] = float(popdata[i][j])

Like this youre list stays altered in the end.

I was definitely not aware of that. Thanks. This the third program I have written ever and I am still getting used to things like that. Ok, so now all of my numbers are floats. However my replacement code is still not working. What I have right now is

popdata2=[0 if x<0.05 else x for x in popdata1]
print(popdata2)

where popdata1 is my array of floats. Would you have any idea why it is not replacing things less than 0.05 with a 0. thanks!

Here it is.

popdata2 = [x if x > 0.05 else 0 for x in popdata1]
print(popdata2)

Edited 6 Years Ago by Beat_Slayer: n/a

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']

Edited 6 Years Ago by pyTony: n/a

I'm still not able to use this code. I am wondering exactly what x is and if maybe my program doesn't know what x is, and that is why I am having the problem. When I do float(x) it tells me that needs to be a string or a number and all of the items in popdata are strings. Could it be that when I say x it thinks the entire list that is popdata[0] etc.?

Tell us what popdata is.
Is it a list of lists?

If so, you'd need something like this:

popdata = [['0' if float(x)<0.21 else x for x in l] for l in popdata]

Edited 6 Years Ago by jcao219: n/a

Ahh yes! that was the problem. I was telling it to check if a list was less than 0.05. Thanks so much for all of your help!

This question has already been answered. Start a new discussion instead.