TypeError when trying to sort a string of a number and a string
See original GitHub issuePython 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:
- Created 9 years ago
- Comments:18 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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()
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.