Details about pythons set implementation

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jan 4 23:15:01 CET 2008


On Fri, 04 Jan 2008 09:29:50 -0800, bukzor wrote:

> Why cant you implement < for complex numbers? Maybe I'm being naive, but
> isn't this the normal definition?
>     a + bi < c + di iff sqrt(a**2 + b**2) < sqrt(c**2, d**2)

No, it is not. Ordered comparisons are not defined for complex numbers. 
Which is bigger, 4+2j or 2+4j?


> How do you implement a set without sorting?

With a hash table.

Or if you are willing to limit yourself to sets of small integers, you 
can implement it using bit flipping. E.g. 5 is an element of the set if 
bit 5 is on.


> Are you expecting better than O(log n)?

Sure.



-- 
Steven



More information about the Python-list mailing list