2

I've spent quite some time searching to see if there was a similar issue. Unfortunately, I couldn't just comment on the previously asked question to see why my attempt was not working. On this site there exists a question where a person wants to sort a list, according to closest numbers to a specific value. his example:not verbatim copying his example but putting a working solution

list_x = [1,2,5,11]
list_x.sort(key = lambda x: abs(x-4))

which will return the list sorted by values closest to 4 (5,2,1,11) However, when trying to sort a much larger list by values closest to zero this is happening to me:

listA.sort(key = lambda x: abs(x))

Traceback (most recent call last):
  File "<pyshell#382>", line 1, in <module>
    listA.sort(key = lambda x: abs(x))
  File "<pyshell#382>", line 1, in <lambda>
    listA.sort(key = lambda x: abs(x))
TypeError: bad operand type for abs(): 'str'

What exactly am I doing incorrectly thats preventing the list from being sorted by absolute values?

This is input data for what makes listA

for i in decrange(0, 13, i):
   code = (func(seq, float(i)))
   codes = '%.3f' % code
   listA.append(float(codes))
   listB.append(str(codes))
listA.sort(key = lambda k: abs(k))
x = listA[0]
answer = listB.index(x) * float(.01)
5
  • 5
    you have strings in your 'larger' list... Commented Apr 24, 2012 at 14:43
  • The input data would be helpful here. You may also call abs(int(x)). Commented Apr 24, 2012 at 14:43
  • Thanks for editting it, I was in middle of the process of trying to fix it XD. Commented Apr 24, 2012 at 14:46
  • 2
    Googling for "bad operand type for abs(): 'str'" gives a helpful answer on the very first result. Commented Apr 24, 2012 at 14:48
  • decrange is just a range function allowing for smaller steps, i =.01 in this case Commented Apr 24, 2012 at 14:48

1 Answer 1

5

The error you're getting seems fairly self-explanatory:

TypeError: bad operand type for abs(): 'str'

It looks like somewhere in your list you have a string value instead of a numeric value. This will work (assuming integer values):

listA.sort(key = lambda x: abs(int(x)))

...but it would be better to figure out why your list has strings in it and fix that instead.

Sign up to request clarification or add additional context in comments.

4 Comments

I thought by floating codes it would convert it to values. listA.append(int(codes)) then would be the correct coding I suppose?
Calling float() should accomplish the same thing. And using int() vs float() depends on whether you expect integer values or floating point values.
thanks for pointing this out those larsks, been working on this program for quite some time. Only programmed simple perl like 8 years ago, and taking a python course. Appreciate the assistance
If this answer helped out, please take a moment to mark it as accepted by clicking on the check mark to the left of the answer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.