[Tutor] Debugging a sort error.
Steven D'Aprano
steve at pearwood.info
Sun Jan 13 05:34:24 EST 2019
On Sun, Jan 13, 2019 at 01:16:10PM +1100, mhysnm1964 at gmail.com wrote:
> Issue, following error is generated after trying to sort a list of strings.
>
> description.sort()
> TypeError: unorderable types: float() < str()
That tells you that you don't have a list of strings. You have a list of
strings with at least one float mixed in.
> There is over 2000 such entries. This used to work and now doesn't. Only
> change to the code was modifying the data variable from a list to a
> dictionary.
I doubt that. Anytime people say "nothing changed except..." it
invariably turns out to be "oh yeah, these other ten things changed
too..."
*wink*
> Below is the full code:
Not it isn't, because the code you supply has at least one syntax error.
So it is impossible for it to be the actual code you are running.
In any case, we are volunteers, not paid consultants. If you want to pay
us $200 an hour to debug your code, you get to dump a big ball of mud in
our laps and say "fix it". But as volunteers, we have no obligation to
trawl through your code trying to debug it (unless it looks interesting,
or easy, or we're bored and looking for something to do...)
To maximize your chances of people actually helping, please read this:
http://www.sscce.org/
Having said that, I have spotted one obvious bug:
> description = data['Description']
>
> for i in description:
> if not str(i):
> print "not a string")
(See what I mean about a syntax error?)
That does not do what you think it does. My *guess* is that you are
trying to check that every item in description is a string. But that's
not what you do: you run through the items, make a string copy of each
one (regardless of what kind of object it is) and then test whether the
string is not the empty string (which it never will be, unless the
original was the empty string).
Instead, try this:
for item in description:
if not isinstance(item, str):
print(type(item), repr(item), "is not a string")
--
Steve
More information about the Tutor
mailing list