On Dec 2, 2019, at 22:40, Random832 <random...@fastmail.com> wrote:
> The OP wanted to return the object that was in the set. I don't think there's 
> currently a way to get it in O(1) time

Yeah, intersection is basically just {key for key in smaller_set if key in 
larger_set}, so it’s always going to return the wrong thing—unless set is 1 
element long, in which case it depends whether you do set&{k} or {k}&set.

I don’t think there’s any way to do this in better than linear time without 
access to the internals of the table. But it would be trivial to add to 
setobject.c. (It’s the same as contains,  but instead of comparing entry->key 
to the key, you just return it.)

Given that it’s occasionally useful, easy to implement on the type, and 
impossible to implement from outside, maybe it’s worth adding. I don’t think it 
should be spelled s[key], because that’s confusing. But a method s.lookup(key) 
that returned the member equal to key or raised KeyError doesn’t seem like it 
would confuse anyone.

(Meanwhile, if you need this behavior in Python today, and you can’t accept 
linear time, but can accept a significant constant multiplier, you could always 
grab the old 2.3 sets module, change the underlying dict from mapping each key 
to None to instead map it to itself, and then the method is just return 

Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
Message archived at 
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to