<div dir="ltr">Perhaps being pedantic, but there is not necessarily ONE key in the original collection which is equal to the search value:<div><br></div><div>>>> d = {2: 'a', 5.0: 'b', 7.2: 'low', 7.6: 'high'}</div>
<div><div>>>> class FuzzyNumber(float):</div><div>...     def __eq__(self, other):</div><div>...         return abs(self-other) < 0.5</div><div>...</div><div>>>> fn = FuzzyNumber(7.5)</div></div><div>
>>> getexact(d, fn) # What gets returned here?!</div><div>7.6</div><div><br></div><div>ANY implementation of this idea would either have to pick the arbitrary first match, or ... well, something else.  Equality isn't actually transitive across all Python objects... and even my quick example isn't a completely absurd data type (it would need to be fleshed out better, but a FuzzyNumber could well have sensible purposes).</div>
<div><br></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Sep 14, 2013 at 9:52 AM, David Mertz <span dir="ltr"><<a href="mailto:mertz@gnosis.cx" target="_blank">mertz@gnosis.cx</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">One way you can spell it currently is:<div><br></div><div><div>>>> getexact = lambda m, v: [x for x in m if x==v][0]</div>
<div class="im"><div>>>> d = {2: 'a', 5.0: 'b'}</div></div><div>>>> s = {2, 5.0}</div>
<div>>>> getexact(d, 2.0)</div><div>2</div><div>>>> getexact(d, 17)</div><div class="im"><div>Traceback (most recent call last):</div><div>  File "<stdin>", line 1, in <module></div>
</div><div>  File "<stdin>", line 1, in <lambda></div>
<div>IndexError: list index out of range</div><div>>>> getexact(s, 2.0)</div><div>2</div><div><br></div></div><div>It's true that the exception leaves a little to be desired here.  If you actually want to use this function much, and worry about the extra equality comparisons in my one-line version, maybe:</div>

<div><br></div><div><div>def getexact(m, v):</div><div>    for x in m:</div><div>        if x==v: return x</div><div>    else:</div><div>        raise KeyError(v)</div></div><div><br></div><div>Like Victor, I'd want to see an actual use case before wanting these as extra methods of the actual data types.  My function versions have the advantage also that they work on ANY iterable, not only dict and set, and also hence have one spelling for the same conceptual operation.</div>

</div><div class="gmail_extra"><div><div class="h5"><br><br><div class="gmail_quote">On Sat, Sep 14, 2013 at 8:09 AM, Serhiy Storchaka <span dir="ltr"><<a href="mailto:storchaka@gmail.com" target="_blank">storchaka@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I propose to add two methods:<br>
<br>
dict.getkey(key) returns original key stored in the dict which is equal to specified key. E.g.<br>
<br>
>>> d = {2: 'a', 5.0: 'b'}<br>
>>> d.getkey(2.0)<br>
2<br>
>>> d.getkey(5)<br>
5.0<br>
>>> d.getkey(17)<br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br>
KeyError: 17<br>
<br>
set.get(value) returns original value stored in the set which is equal to specified value. E.g.<br>
<br>
>>> s = {2, 5.0}<br>
>>> s.get(2.0)<br>
2<br>
>>> s.get(5)<br>
5.0<br>
>>> s.get(17)<br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br>
KeyError: 17<br>
<br>
______________________________<u></u>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/<u></u>mailman/listinfo/python-ideas</a><br>
</blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br>Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>
uneducated; technology from the underdeveloped; and putting <br>
advocates of freedom in prisons.  Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br>
</font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>
advocates of freedom in prisons.  Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br>
</div>