Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51119 > unrolled thread

Re: Python 3: dict & dict.keys()

Started byPeter Otten <__peter__@web.de>
First post2013-07-24 08:23 +0200
Last post2013-07-24 12:54 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Python 3: dict & dict.keys() Peter Otten <__peter__@web.de> - 2013-07-24 08:23 +0200
    Re: Python 3: dict & dict.keys() Neil Cerutti <neilc@norwich.edu> - 2013-07-24 12:54 +0000

#51119 — Re: Python 3: dict & dict.keys()

FromPeter Otten <__peter__@web.de>
Date2013-07-24 08:23 +0200
SubjectRe: Python 3: dict & dict.keys()
Message-ID<mailman.5026.1374646976.3114.python-list@python.org>
Ethan Furman wrote:

> So, my question boils down to:  in Python 3 how is dict.keys() different
> from dict?  What are the use cases?

I just grepped through /usr/lib/python3, and could not identify a single 
line where some_object.keys() wasn't either wrapped in a list (or set, 
sorted, max) call, or iterated over.

To me it looks like views are a solution waiting for a problem.

[toc] | [next] | [standalone]


#51128

FromNeil Cerutti <neilc@norwich.edu>
Date2013-07-24 12:54 +0000
Message-ID<b5a14cFkachU1@mid.individual.net>
In reply to#51119
On 2013-07-24, Peter Otten <__peter__@web.de> wrote:
>> So, my question boils down to:  in Python 3 how is dict.keys()
>> different from dict?  What are the use cases?
>
> I just grepped through /usr/lib/python3, and could not identify
> a single line where some_object.keys() wasn't either wrapped in
> a list (or set, sorted, max) call, or iterated over.
>
> To me it looks like views are a solution waiting for a problem.

Here's a case of using keys as a set-like view from my own
"production" code (i.e., I used it once for one important job):

seen = set()
students = {}
dates = []

for fname in sorted(glob.glob("currentterm201320?.csv")):
    print(fname, end="\n\t")
    date = get_date(fname)
    dates.append(date)
    term = fname[-11:-4]
    r = reg.registration(term, path=".")
    regs = r.keys()
    for alt_id in regs & seen:
        students[alt_id].append(r[alt_id])
    for alt_id in seen - regs:
        students[alt_id].append(None)
    for alt_id in regs - seen:
        students[alt_id] = [None]*(len(dates)-1) + [r[alt_id]]
        seen.add(alt_id)

It was a very nice way to to do three different things depending
on the student sin the set I was working with, compared to a
registration list:

Granted the line was originally "regs = set(regs.keys())" before
it occurred to me that it sucked to take what must be equivalent
to a set, convert to a list, and then back to set again.

Thanks to the set-like view of dict.keys it worked just like one
might hope.

Looking at it again "seen" might be a redundant parallel version
of students.keys().

-- 
Neil Cerutti

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web