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

Recommended Answers

All 17 Replies

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]

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?

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?

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)

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

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]

yes popdata is a list of lists.

yes popdata is a list of lists.

So which list(s) from popdata do you want to change?

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.