Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #93064
| Path | csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'python3': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.14; '2.0:': 0.16; 'dictionary,': 0.16; 'key):': 0.16; 'keys.': 0.16; 'other:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'storing': 0.16; 'subject:key': 0.16; 'wrote:': 0.16; 'int,': 0.22; 'suppose': 0.22; '2.0': 0.23; 'second': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'cat': 0.29; 'dan': 0.29; 'key,': 0.29; 'values': 0.30; 'skip:_ 10': 0.32; 'getting': 0.33; 'class': 0.33; 'usually': 0.33; 'subject:?': 0.34; 'to:addr:python-list': 0.35; 'false': 0.35; 'but': 0.36; 'being': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'received:de': 0.40; 'some': 0.40; 'strange': 0.63; 'sounds': 0.72; 'fields,': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Looking up a dictionary _key_ by key? |
| Date | Wed, 24 Jun 2015 08:44 +0200 |
| Organization | None |
| References | <CAGGBd_pd9SA7SczanGARFr-+Z9SxP9Mkhfo-RivpcJLrnuexng@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd8d85.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.6.1435128250.3674.python-list@python.org> (permalink) |
| Lines | 40 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1435128250 news.xs4all.nl 2837 [2001:888:2000:d::a6]:49514 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:93064 |
Show key headers only | View raw
Dan Stromberg wrote:
> I know that sounds strange: usually we look up values by key, not keys.
>
> But suppose you have a strange key type that despite being "equal", is
> not identical in some fields, and you need to see those fields.
>
> Is there a way of getting the key used by the dictionary, short of
> storing a reference to it in the value, or using a second dictionary?
$ cat grab_key.py
class GrabKey:
def __init__(self, key):
self.key = key
def __eq__(self, other):
if self.key == other:
self.dict_key = other
return True
return False
def __hash__(self):
return hash(self.key)
def grab_key(k, d):
g = GrabKey(k)
d[g]
return g.dict_key
if __name__ == "__main__":
d = {1: int, 2.0: float}
print(grab_key(1.0, d))
print(grab_key(2, d))
$ python3 grab_key.py
1
2.0
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Looking up a dictionary _key_ by key? Peter Otten <__peter__@web.de> - 2015-06-24 08:44 +0200
csiph-web