question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError when trying to sort a string of a number and a string

See original GitHub issue

Python 3.3 and Python 3.4 fail to sort collections that contain strings that are also numbers:

>>> import natsort
>>> natsort.natsorted(('a', '1'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/kwpolska/virtualenvs/nikola-py3/lib/python3.4/site-packages/natsort/natsort.py", line 247, in natsorted
    return sorted(seq, key=lambda x: natsort_key(key(x),
TypeError: unorderable types: float() < str()

This works just fine in Python 2.7.6.

via getnikola/nikola#1275 (cc @devurandom)

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ralsinacommented, May 7, 2014

You can use key(). For example, you can always compare tuples. The behaviour of sorted() is to compare elements left-to-right, and decide on the first one that’s not equal. So, to compare floats and strings, you could compare (‘string’, ‘1’) to (‘float’, 2.0). You can even generate the first element by using type()

0reactions
fake-namecommented, Jun 23, 2014

I actually left it running for a while, and it made it to ~35 million items before I got bored and stopped it.

It doesn’t seem like it’s really worth bothering pull requesting for, just feel free to copy & paste it wherever.

I’m not sure how one would integrate a actual random-fuzzer into a unit-testing system. They’re kind of intrinsically non-repeatable, so they’re not guaranteed to catch issues. I guess you could set it up so it just runs a number of iterations. You would need some mechanism to log the string that triggers failure, though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

' not supported between instances of 'int' and 'str' , Python ...
It sounds like your 'Name' column has mixed data types - some strings and some integers. You can treat the integers as strings...
Read more >
How to Use sorted() and sort() in Python - Real Python
Sorting Numbers; Sorting Strings ... The same TypeError is thrown when you try to compare two non-comparable values without using sorted() .
Read more >
TypeError: cannot use 'in' operator to search for 'x' in 'y'
The in operator can only be used to check if a property is in an object. You can't search in strings, or in...
Read more >
Built-in Types — Python 3.11.1 documentation
There are three distinct numeric types: integers, floating point numbers, ... float also accepts the strings “nan” and “inf” with an optional prefix...
Read more >
TypeError: 'int' object is not subscriptable - STechies
Python Range · Python : end parameter in print() · Id() function in Python · Python String Title() Method · Install Opencv Python...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found