Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #93054
| Path | csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <drsalists@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'value,': 0.03; 'indexing': 0.07; '[1,': 0.09; 'typeerror:': 0.09; 'cc:addr:python-list': 0.10; '23,': 0.16; 'cc:name:python list': 0.16; 'dictionary,': 0.16; 'key?': 0.16; 'received:mail-qk0-x22a.google.com': 0.16; 'storing': 0.16; 'subject:key': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; '"",': 0.22; 'keys': 0.22; '2015': 0.23; 'header:In-Reply-To:1': 0.24; '(most': 0.24; 'second': 0.24; 'message-id:@mail.gmail.com': 0.28; 'dan': 0.29; 'dictionary': 0.29; 'knows': 0.32; 'getting': 0.33; 'traceback': 0.33; 'thanks!': 0.34; 'subject:?': 0.34; 'file': 0.34; 'received:google.com': 0.34; 'there': 0.36; 'subject:: ': 0.37; 'tue,': 0.38; 'pm,': 0.39; 'method': 0.39; 'does': 0.39; 'provide': 0.61; 'request.': 0.66; '8bit%:40': 0.66; 'skip:\xe2 10': 0.70; 'skip:/ 30': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=V3lOxD46ARrCGZZnLJTNyJgJyiE1xoGVMS2e74NEdOA=; b=RVofdG677ZEGrTyMgJAVOr3Ols2XHYgM/3ycDvQ/mqjBD3ag+89fi5ttwuQR7jDFvM 6cFmhNMhktf2MG0NNekzeLEhdRqvrFTeq/lTGHm6/ZhZYkiOKxVkafK7KL3/yPPVZ/BC Q4C1Za4wqcjsM79ydVQm1i/RNOvqfWL+fan5C+aZ+mEFJnWiq1iEw1Z1kVnpY5JJfwUp lE1pPVq86w7xvYiHNmWXKF5eYT0EuUrGqzhiUa6s+2hX8k7Phf4+fJUSbWXtjn3Xate7 50hKpHQpKwtn+Xg+I15IVBwBbWdYXCPtMxkr/RzyhedBvXKgx7DPPOzYdFvnPP+4WNx7 gaCA== |
| MIME-Version | 1.0 |
| X-Received | by 10.55.15.91 with SMTP id z88mr7453133qkg.87.1435106878721; Tue, 23 Jun 2015 17:47:58 -0700 (PDT) |
| In-Reply-To | <851th2t05t.fsf@benfinney.id.au> |
| References | <CAGGBd_pd9SA7SczanGARFr-+Z9SxP9Mkhfo-RivpcJLrnuexng@mail.gmail.com> <851th2t05t.fsf@benfinney.id.au> |
| Date | Tue, 23 Jun 2015 17:47:58 -0700 |
| Subject | Re: Looking up a dictionary _key_ by key? |
| From | Dan Stromberg <drsalists@gmail.com> |
| To | Ben Finney <ben+python@benfinney.id.au> |
| Cc | Python List <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| 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.2.1435107329.3674.python-list@python.org> (permalink) |
| Lines | 68 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1435107329 news.xs4all.nl 2840 [2001:888:2000:d::a6]:57927 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:93054 |
Show key headers only | View raw
On Tue, Jun 23, 2015 at 5:33 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
> Dan Stromberg <drsalists@gmail.com> writes:
>
>> 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?
>
> The dictionary knows its keys and can provide them on request. Call the
> ‘dict.keys’ method to get them as a collection.
So it appears:
$ ./pythons --command 'd={1:2, 2:4, 3:8}; print(d.keys())'
/usr/local/cpython-2.4/bin/python good [1, 2, 3]
/usr/local/cpython-2.5/bin/python good [1, 2, 3]
/usr/local/cpython-2.6/bin/python good [1, 2, 3]
/usr/local/cpython-2.7/bin/python good [1, 2, 3]
/usr/local/cpython-3.0/bin/python good <dict_keys object at 0x8514fa4>
/usr/local/cpython-3.1/bin/python good dict_keys([1, 2, 3])
/usr/local/cpython-3.2/bin/python good dict_keys([1, 2, 3])
/usr/local/cpython-3.3/bin/python good dict_keys([1, 2, 3])
/usr/local/cpython-3.4/bin/python good dict_keys([1, 2, 3])
/usr/local/cpython-3.5/bin/python good dict_keys([1, 2, 3])
/usr/local/jython-2.7rc1/bin/jython good [3, 2, 1]
/usr/local/pypy-2.5.0/bin/pypy good [1, 2, 3]
/usr/local/pypy3-2.4.0/bin/pypy3 good dict_keys([1, 2, 3])
But:
$ ./pythons --command 'd={1:2, 2:4, 3:8}; print(d.keys()[1])'
/usr/local/cpython-2.4/bin/python good 2
/usr/local/cpython-2.5/bin/python good 2
/usr/local/cpython-2.6/bin/python good 2
/usr/local/cpython-2.7/bin/python good 2
/usr/local/cpython-3.0/bin/python bad
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'dict_keys' object does not support indexing
/usr/local/cpython-3.1/bin/python bad
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'dict_keys' object does not support indexing
/usr/local/cpython-3.2/bin/python bad
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'dict_keys' object does not support indexing
/usr/local/cpython-3.3/bin/python bad
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'dict_keys' object does not support indexing
/usr/local/cpython-3.4/bin/python bad
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'dict_keys' object does not support indexing
/usr/local/cpython-3.5/bin/python bad
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'dict_keys' object does not support indexing
/usr/local/jython-2.7rc1/bin/jython good 2
/usr/local/pypy-2.5.0/bin/pypy good 2
/usr/local/pypy3-2.4.0/bin/pypy3 bad
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'dict_keys' object is not subscriptable
Would I have to do an O(n) search to find my key?
Thanks!
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Looking up a dictionary _key_ by key? Dan Stromberg <drsalists@gmail.com> - 2015-06-23 17:47 -0700
csiph-web