[Tutor] Debugging a sort error.

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Sun Jan 13 17:29:48 EST 2019


All,
Once again thanks for all the suggestions. It was the input data after all. As I am importing three sheets into python. One of the sheets had one less column. Thus how I ended up with float points. The testing for the string helped here greatly.  Now things are correct again. 

On and forward to start working on text pattern exercise which I always have struggled with. Last language I did this in was Perl and had all sorts of headaches. 😊 Python seems cleaner from the reading I have done thus far. Lets see what challenges wait in front of me.




-----Original Message-----
From: Stephen Nelson-Smith <sanelson at gmail.com> 
Sent: Monday, 14 January 2019 1:15 AM
To: mhysnm1964 at gmail.com
Cc: Python Tutor mailing list <Tutor at python.org>
Subject: Re: [Tutor] Debugging a sort error.

Hi,

On Sun, Jan 13, 2019 at 8:34 AM <mhysnm1964 at gmail.com> wrote:

> description.sort()
> TypeError: unorderable types: float() < str()

So, fairly obviously, we can't test whether a float is less than a string.  Any more than we can tell if a grapefruit is faster than a cheetah.  So there must be items in description that are strings and floats.

With 2000 lines, you're going to struggle to eyeball this, so try something like this:

In [69]: irrational_numbers = [3.14159265, 1.606695, "pi", "Pythagoras Constant"] In [70]: from collections import Counter In [71]: dict(Counter([type(e) for e in irrational_numbers]))
Out[71]: {float: 2, str: 2}

If with your data, this shows only strings, I'll eat my hat.

S.



More information about the Tutor mailing list