RE: Two variable dictionary comprehension

2017-04-04 Thread Deborah Swanson
Rob Gaddi wrote, on Tuesday, April 04, 2017 3:56 PM
> 
> On 04/04/2017 03:34 PM, Deborah Swanson wrote:
> > Terry Reedy wrote, on Tuesday, April 04, 2017 11:04 AM
> >>
> >> I am sure that this is because list comprehensions were once thr
only 
> >> comprehensions and that the index was not updated when the syntax
and 
> >> code was.  I opened issue 29981 "Update Index for set, dict, and 
> >> generator 'comprehensions'".
> >>
> >> --
> >> Terry Jan Reedy
> >
> > Thanks Terry. I does seem strange that there are so many types of 
> > comprehensions, yet under 'comprehensions' in the Index, you only
see 
> > 'list'. And there are no entries for the other types, by their
names.
> >
> > Deborah
> >
> 
> Does it seem... incomprehensible?
> 
> -- 
> Rob Gaddi, Highland Technology -- www.highlandtechnology.com 
> Email address domain is currently out of order.  See above to fix.

Ha ha! Indeed it does. Funny how 'comprehension' word forms so easily
fall into puns.

Deborah

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-04 Thread Deborah Swanson
Sorry I bailed on you last night, but you know it's bad when you can't
read anymore.

I read through this today and saw several things I really need to work
with more, especially dicts, sets, generators and the zip function. I've
used all of them in fairly typical forms, but it's the atypical ones
that throw me. And strangely enough, I've never seen dicts described as
having key:value syntax, although it's certainly true and clarifies a
lot in thinking about them.

The one thing you mentioned that was completely new to me was the use of
an if clause in a list comprehension, and I can see there being times
when that's exactly what you want to do. I'll have to try the other
types of comprehensions and see if this same grammar works in all of
them. It should. Interesting how computer languages seem to be a hybrid
of math and linguistics.

I added all of these concepts and code snippets to my study list. Thanks
again Steve.  ;)

Deborah


Steve D'Aprano wrote, on Monday, April 03, 2017 6:05 PM
> 
> On Tue, 4 Apr 2017 03:27 am, Deborah Swanson wrote:
> 
> > I'll admit that both dictionaries and comprehensions are still a 
> > little bit fuzzy to me, especially when I get away from the common 
> > usages. This could be a good exercise to clarify some of the fuzzy 
> > areas.
> 
> 
> As far as comprehensions go, how is your mathematics?
> 
> If you remember your set builder notation from maths, list 
> comprehensions are based on that.
> 
> In maths, we say something like:
> 
> {2n + 1 : 0 ≤ n ≤ 9}
> 
> which might be read aloud as "the set of 2 times n, plus 1, 
> such that n is between 0 and 9 inclusive".
> 
> http://www.mathwords.com/s/set_builder_notation.htm
> 
> A similar notation might be:
> 
> {2n + 1 : n ∈ {1, 2, 3}}
> 
> said as "the set of 2 times n, plus 1, such that n is an 
> element of the set {1, 2, 3}".
> 
> 
> If you're a maths geek like me, this is second nature :-)
> 
> 
> Now, Python's list comprehension syntax is based on a similar 
> notation, except we spell it out in words rather than 
> symbols, using the familiar "for n in ..." syntax from 
> for-loops instead of : "such that".
> 
> {2*n + 1 for n in range(10)}
> 
> {2*n + 1 for n in (1, 2, 3)}
> 
> 
> A few other differences:
> 
> - list comprehensions use [ ] for the outermost brackets;
> - set and dict comprehensions use { };
> - generator expressions use ( ) (unless the parentheses can 
> be implied).
> 
> We're not restricted to mathematical expressions, and can use 
> any Python expression we like:
> 
> [value.method(arg)[1] for (value, arg) in zip(values, arguments)]
> 
> 
> translates roughly into this for-loop:
> 
> result = []
> for (value, arg) in zip(values, arguments):
> result.append(value.method(arg)[1])
> 
> 
> 
> Another difference is that comprehensions allow an "if" clause:
> 
> [v.method(a)[1] for (v, a) in zip(values, arguments) if v is not None]
> 
> translates something like:
> 
> 
> result = []
> for (v, a) in zip(values, arguments):
> if v is not None:
> result.append(v.method(a)[1])
> 
> 
> There's more, but that covers probably 98% of comprehension usage.
> 
> And of course, remember that dict comprehensions use the same 
> key:value syntax as ordinary dict "literals" (the official 
> name is "dict display").
> 
> result = {}
> for key, value in zip(keys, values):
> result[key] = value
> 
> 
> becomes
> 
> result = {key:value for (key, value) in zip(keys, values)}
> 
> although frankly that's better written as:
> 
> dict(zip(keys, values))
> 
> 
> 
> Hope this helps!
> 
> 
> 
> 
> -- 
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered 
> up, and sure enough, things got worse.
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-04 Thread Rob Gaddi

On 04/04/2017 03:34 PM, Deborah Swanson wrote:

Terry Reedy wrote, on Tuesday, April 04, 2017 11:04 AM


On 4/3/2017 2:35 AM, Gregory Ewing wrote:

Deborah Swanson wrote:


Oh, come on. That's a fairly obscure citation in the docs,

one that

would take a good deal of experience and time reading

through them to

know was there,



Having said that, the index of the Python docs could be

improved a bit

in this area -- currently it only mentions "list" under
"comprehension" (although the page it leads to discusses the other
types as well).


I am sure that this is because list comprehensions were once thr only
comprehensions and that the index was not updated when the syntax and
code was.  I opened issue 29981 "Update Index for set, dict, and
generator 'comprehensions'".

--
Terry Jan Reedy


Thanks Terry. I does seem strange that there are so many types of
comprehensions, yet under 'comprehensions' in the Index, you only see
'list'. And there are no entries for the other types, by their names.

Deborah



Does it seem... incomprehensible?

--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
--
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-04 Thread Deborah Swanson
Terry Reedy wrote, on Tuesday, April 04, 2017 11:04 AM
> 
> On 4/3/2017 2:35 AM, Gregory Ewing wrote:
> > Deborah Swanson wrote:
> >
> >> Oh, come on. That's a fairly obscure citation in the docs, 
> one that 
> >> would take a good deal of experience and time reading 
> through them to 
> >> know was there,
> 
> > Having said that, the index of the Python docs could be 
> improved a bit 
> > in this area -- currently it only mentions "list" under 
> > "comprehension" (although the page it leads to discusses the other 
> > types as well).
> 
> I am sure that this is because list comprehensions were once thr only 
> comprehensions and that the index was not updated when the syntax and 
> code was.  I opened issue 29981 "Update Index for set, dict, and 
> generator 'comprehensions'".
> 
> -- 
> Terry Jan Reedy

Thanks Terry. I does seem strange that there are so many types of
comprehensions, yet under 'comprehensions' in the Index, you only see
'list'. And there are no entries for the other types, by their names.

Deborah

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-04 Thread Terry Reedy

On 4/3/2017 6:09 PM, Deborah Swanson wrote:

Nathan Ernst wrote, on April 03, 2017 1:59 PM


I was a bit surprised when I looked at the language reference
for 3.6.x. I expected there'd be a direct link to
comprehensions, but there's not.

...

FWIW, If one was completely new to Python, even knowing the
syntax is known as a "comprehension" might be unknown. I
certainly didn't know what a comprehension was when I was
learning Python. A coworker showed me, some 13 years ago.



Thanks Nate, for your comprehension of the plight of many, if not most,
newish Python coders. And it certainly doesn't help to ask the list to
fill in some of the holes and be met with criticism for asking, but I
digress. It is what it is.


I have more than once looked at the Reference TOC: Expressions
https://docs.python.org/3/reference/index.html
and wondered 'where are comprehensions?' (and a few other things).  I 
just opened Issue 29983: "Reference TOC: expand 'Atoms' and 
'Primaries'", https://bugs.python.org/issue29983, suggesting that


6.2. Atoms
6.3. Primaries

be expanded to

6.2. Atoms, including identifiers, literals, and comprehensions
6.3. Primaries: attributes, subscripts, slices, and calls

The 'primaries' list is complete.  The full 'atoms' list is too long so 
I picked what I thought might be the three most important and added 
'including' to indicate that this is a selection.


There might be opposition (or just +-0 indifference) from developers who 
are CS language theory wonks who have forgotten how obscure the terms 
'atom' and 'primary' are in this context.  So anyone supporting this 
should say so on the issue.


https://bugs.python.org/issue29981, adding index entries, should be 
routine, but at least one +1 would be helpful.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-04 Thread Terry Reedy

On 4/3/2017 2:35 AM, Gregory Ewing wrote:

Deborah Swanson wrote:


Oh, come on. That's a fairly obscure citation in the docs, one that
would take a good deal of experience and time reading through them to
know was there,



Having said that, the index of the Python docs could be
improved a bit in this area -- currently it only mentions
"list" under "comprehension" (although the page it leads
to discusses the other types as well).


I am sure that this is because list comprehensions were once thr only 
comprehensions and that the index was not updated when the syntax and 
code was.  I opened issue 29981 "Update Index for set, dict, and 
generator 'comprehensions'".


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-04 Thread Deborah Swanson
Thanks Steve for writing this, and I'll read it more carefully when my
illness gives my mind back to me.

I was actually a math major before I discovered computer science, and I
had to progress beyond machine language and assembler before I found the
subtle differences and more flexible boundaries of higher level
programming languages you talk about. I'm sure much of it's the same
with Python. They key I think will be in finding more of the logic in
Python, and then fleshing it out with more detail and choices. The core
logic wasn't taught to me in the courses I took, so I need to backfill
it now.

I look forward to studying what you've written here as soon as I can.

Deborah


Steve D'Aprano wrote, on Monday, April 03, 2017 6:05 PM
> 
> On Tue, 4 Apr 2017 03:27 am, Deborah Swanson wrote:
> 
> > I'll admit that both dictionaries and comprehensions are still a 
> > little bit fuzzy to me, especially when I get away from the common 
> > usages. This could be a good exercise to clarify some of the fuzzy 
> > areas.
> 
> 
> As far as comprehensions go, how is your mathematics?
> 
> If you remember your set builder notation from maths, list 
> comprehensions are based on that.
> 
> In maths, we say something like:
> 
> {2n + 1 : 0 ≤ n ≤ 9}
> 
> which might be read aloud as "the set of 2 times n, plus 1, 
> such that n is between 0 and 9 inclusive".
> 
> http://www.mathwords.com/s/set_builder_notation.htm
> 
> A similar notation might be:
> 
> {2n + 1 : n ∈ {1, 2, 3}}
> 
> said as "the set of 2 times n, plus 1, such that n is an 
> element of the set {1, 2, 3}".
> 
> 
> If you're a maths geek like me, this is second nature :-)
> 
> 
> Now, Python's list comprehension syntax is based on a similar 
> notation, except we spell it out in words rather than 
> symbols, using the familiar "for n in ..." syntax from 
> for-loops instead of : "such that".
> 
> {2*n + 1 for n in range(10)}
> 
> {2*n + 1 for n in (1, 2, 3)}
> 
> 
> A few other differences:
> 
> - list comprehensions use [ ] for the outermost brackets;
> - set and dict comprehensions use { };
> - generator expressions use ( ) (unless the parentheses can 
> be implied).
> 
> We're not restricted to mathematical expressions, and can use 
> any Python expression we like:
> 
> [value.method(arg)[1] for (value, arg) in zip(values, arguments)]
> 
> 
> translates roughly into this for-loop:
> 
> result = []
> for (value, arg) in zip(values, arguments):
> result.append(value.method(arg)[1])
> 
> 
> 
> Another difference is that comprehensions allow an "if" clause:
> 
> [v.method(a)[1] for (v, a) in zip(values, arguments) if v is not None]
> 
> translates something like:
> 
> 
> result = []
> for (v, a) in zip(values, arguments):
> if v is not None:
> result.append(v.method(a)[1])
> 
> 
> There's more, but that covers probably 98% of comprehension usage.
> 
> And of course, remember that dict comprehensions use the same 
> key:value syntax as ordinary dict "literals" (the official 
> name is "dict display").
> 
> result = {}
> for key, value in zip(keys, values):
> result[key] = value
> 
> 
> becomes
> 
> result = {key:value for (key, value) in zip(keys, values)}
> 
> although frankly that's better written as:
> 
> dict(zip(keys, values))
> 
> 
> 
> Hope this helps!
> 
> 
> 
> 
> -- 
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered 
> up, and sure enough, things got worse.
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-04 Thread Deborah Swanson
Gregory Ewing wrote, on Monday, April 03, 2017 4:23 PM
> 
> Deborah Swanson wrote:
> > I'd
> > imagine that the old Google would have returned a good 10 pages or 
> > more (probably a lot more) of urls containing the phrase "dict 
> > comprehension" or "dictionary comprehension".
> 
> It still does, as far as I can see. I just googled for "dict 
> comprehension", and the vast majority of results in the first 
> 10 pages relate to Python.
> 
> By page 20 it's starting to wander off a bit, but you can 
> hardly blame it for that. There *are* non-Python web pages 
> that mention the words "dict" and "comprehension", and how is 
> Google to know that you don't want those if you don't tell it?

Yes, I searched on "python dict comprehension" because I'd gottion the
idea somewhere that the "python" was necessary in this Brave New Google
world. I'll remember to omit "python" in the future when searching for
phrases most frequently found in Python. Boolean rules were so much
easier to learn and remember.
 
> > You used to be able to keep sifting through pages of results after
the 
> > bulk of urls fitting your criteria had passed, and still find useful

> > things to look at, sometimes at page 500
> 
> Seems to me Google was doing a rather *bad* job if you had
> to wade through 500 pages of results to find what you wanted.
> I would never have the patience to do that!

Well, it helped if you used 100 search results per page and other
various and sundry tools. But patience was good too. It depended how bad
you wanted the answer and how good a skim reader you are. Now I rarely
find anything I want unless I'm shopping for something to buy, but even
then I have to wade through all the garbage results intended to entice
the impulse buyer in me, which I ditched decades ago.
 
> Anyhow, the reason Google got brought up was that you were 
> complaining about difficulty of finding things in the Python 
> docs. Google *does* turn up the relevant part of the docs in 
> the very first page of results, so being able to do a direct 
> text search on the docs wouldn't do any better.

We can duke that out some other time. My illness is overcoming me fast
though, so it won't be right away. 

Deborah

> -- 
> Greg

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-03 Thread Deborah Swanson
Gregory Ewing wrote, on Monday, April 03, 2017 4:23 PM
> 
> Deborah Swanson wrote:
> > All my guesses were based on the
> > single variable (the most common type) examples I found. I just
didn't 
> > think of putting a colon after 'label', and found nothing to suggest

> > that's what I should do.
> 
> Hmmm, I'm not sure what the docs could do to make that any 
> clearer. The key:value syntax is part of *every* dict 
> comprehension (it's the only thing that distinguishes a dict 
> comprehension from a set comprehension).

I guess I saw a lot of examples that weren't clearly using the key:value
syntax, and all of it was so unfamiliar, that pattern just didn't stand
out to me. But it's starting to now, and that's the direction I need to
go in for dict comprehensions.

> > Despite my earlier words and protestations that I did look for two 
> > variable dict comprehensions, fairly diligently, I am taking what
you 
> > said seriously.
> 
> Don't feel too bad. Sometimes it's hard to know what to 
> google for, even for experienced people! Also it's hard for 
> doc writers to anticipate how less experienced people will 
> think. It wouldn't have occurred to me to use the phrase "two 
> variable dict comprehension" when writing documentation.

Yes, I was pretty sure the terminology I phrased the question with
wasn't correct, but I didn't know the right words to say, or where to
look them up, so I just tried to be as descriptive as I could.

But I accomplished my purpose in asking the question, even if it was
poorly put, and I've received quite a bit of good information, the
terminology I couldn't put my finger on, and some solid pointers of
directions to go in the future.
 
> Another thing it's important to be able to do is see through
> to the principles behind things, and then use those 
> principles to solve new problems that aren't explicitly 
> covered in any docs or examples.

Ah, yes, there's the rub. But it's true, from cooking to car mechanics
to Python coding, the key is in becoming familiar with the subject, and
hands on is the best teacher. (Yes, yes, we could have a discussion
about the role of education, but I'll say that it's seeing it all in
motion for yourself many times that seals the teaching into something
you know and don't need to be told.)

> The general principle behind all the forms of comprehension
> is that they consist of a prototypical element, followed by 
> some clauses that generate values for that element.
> 
> The syntax for the prototypical element mirrors that of the 
> corresponding display. So, for a dict comprehension, the 
> prototypical element has the form 'key:value', where 'key' 
> and 'value' are arbitrary expressions. From there, it's just 
> a matter of figuring out what to put into those expressions.
> 
> In your case, another part of the puzzle is the fact that
> you can unpack a tuple obtained from a for-loop into
> variables. That's a general feature of for-loops, both
> inside and outside of comprehensions, so you probably
> won't find it mentioned explicitly under the heading of
> dict comprehensions.
> 
> > Maybe it would be worthwhile to scrape the whole mess and have it in

> > searchable text form.
> 
> The HTML Python docs have a "Search" box, although I haven't 
> really used it so I don't know how well it works. 

It sucks, in a phrase. You're presented with a list of web page titles,
very few of which seem to have much to do with the Python language topic
you type into the search box. If you're interested in theoretical
dissertations you'll be pleased, but if you're looking for barebones
mechanical descriptions of the language you'll be wasting your time, in
my experience.

> In my 
> experience, Google with a site search often works a lot 
> better than search functions provided by sites themselves.

Yes, I used to use Google's site search on a regular basis but somehow
got out of the habit. It likely can't be beat if you know which website
will have the answer you're looking for, but the old Google truly shown
at finding places you didn't already know about.

> > I hope you won't be miffed though if I still come up empty handed
and 
> > come back here to ask again.
> > 
> > Deborah
> > 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-03 Thread Deborah Swanson
Thank you Nate, for all these sources to study. Python was very easy for
me to learn in 2 online courses, but it's been all uphill since then.
I've learned a lot and for that I'm grateful, but there's so much
farther to go.

I've appreciated our discussions, but I am in fact a very sick person
and my illness has come back again tonight. I can probably reply to a
few more posts and then you won't see me again for awhile.

But many thanks again, and I will begin reading the material you
suggested.

Deborah


Nathan Ernst wrote, on Monday, April 03, 2017 3:37 PM

No worries, Deborah.


Python is by most measurements a relatively easy/simple language to
learn, but there are always the dusty corners. If you've not already, I
recommend going through the online Python tutorial in it's entirety
(https://docs.python.org/3/tutorial/index.html).


After that, learn the language syntax that wasn't covered in the
tutorial by reading the Language Reference
(https://docs.python.org/3/reference/index.html). The tutorial should be
fairly easy for a straight beginner to follow. The language reference
assumes a little higher-level understanding of programming language
grammar.  The Python Language Reference uses a modified BNF syntax (BNF
being Backus-Naur form. You can read about BNF at
https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form). To be honest,
I'm not sure what modifications Python uses to BNF, maybe someone else
can shed some light (or skin) on it.


After you'd done those, peruse the standard library. I don't recommend
deep reading there at this point, but at least a cursory reading so
you're cognizant of libraries that are built-in that may help do things
may you want to do now, or in the future (i.e. make a web request, parse
JSON or XML, handle datetimes).


Remember: Python comes with batteries included.


-Nate


On Mon, Apr 3, 2017 at 5:09 PM, Deborah Swanson
 wrote:

Nathan Ernst wrote, on April 03, 2017 1:59 PM
>
> I was a bit surprised when I looked at the language reference
> for 3.6.x. I expected there'd be a direct link to
> comprehensions, but there's not.
>
> You have to know what you're looking for:
>
> 6.2.5: List Displays
> 6.2.6: Set Displays
> 6.2.7: Dictionary Displays
>
> And, then, click on the appropriate element of the sub
> grammar to find the appropriate syntax.
>
> So, it took me about 30 seconds to find the appropriate
> grammars, when I expected it'd only take about 5 seconds,
> since I'm very familiar with the python docs & how the
> grammar documentation is laid out.  I can fully understand
> how someone less familiar with the documentation might have a
> harder time finding the grammar than I did.
>
> FWIW, If one was completely new to Python, even knowing the
> syntax is known as a "comprehension" might be unknown. I
> certainly didn't know what a comprehension was when I was
> learning Python. A coworker showed me, some 13 years ago.
>
> Regards,
> Nate

Thanks Nate, for your comprehension of the plight of many, if not most,
newish Python coders. And it certainly doesn't help to ask the list to
fill in some of the holes and be met with criticism for asking, but I
digress. It is what it is.

Before I started reading the list a few months ago, I'd heard of list
comprehensions in an article I'd read, and hardly understood the gist of
it. But look at me now Ma, I've learned not only how to use list
comprehensions but also a small tribe of other kinds of comprehensions!

(If there's a moral to this story, heck if I know exactly what it is.
"Keep on trying" is as good as any.)

Deborah


> On Mon, Apr 3, 2017 at 3:47 PM, Jerry Hill
>  wrote:
>
> > On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson
> >  wrote:
> > > Regular updates as the docs are updated would be a good idea too.
> > > It's obvious that today's Google isn't up to it, although
> it occurs
> > > to me that I haven't tried Google's site search on python.org.
> >
> > So, when you search google for the phrase "dict comprehension" or
> > "dictionary comprehension", nothing useful comes up for
> you?  When I
> > search either of those phrases, I get lots of useful
> results, all of
> > which spell out how to do what you were originally asking about.  I
> > know Google search results are skewed by past usage, but
> I'm surprised
> > that you didn't find anything useful in the first couple of search
> > results.
> >
> > When I do a search for 'dict comprehension' I get a boxed result
> > linking to PEP 274 as the first hit, then two Stack Overflow
> > questions, both of which demonstrate how to do dictionary
> > comprehensions.  Following that is another link to PEP 274,
> a link to
> > the Python docs on data structures (which does talk about dict
> > comprehensions, but it's way down on the page), and then links to a
> > bunch of tutorials.  If you had to judge based on my search
> results,
> > Google does a fine job of answering python questions, 

Re: Two variable dictionary comprehension

2017-04-03 Thread Jerry Hill
On Mon, Apr 3, 2017 at 5:50 PM, Deborah Swanson
 wrote:
> Ah, but did you actually try to use the proposed solutions on the two
> stackoverflow pages? It's been several weeks now, but I did, and neither
> of those two examples fit my situation, which is why I ended up writing
> my own, and unsatisfactorily at that.

Well, that first Stack Overflow link has the following as the first
part of the highest scored answer:

  In Python 2.6 and earlier, the dict constructor can receive an
iterable of key/value pairs:
  d = dict((key, value) for (key, value) in iterable)

  From Python 2.7 and 3 onwards, you can just use the dict
comprehension syntax directly:
  d = {key: value for (key, value) in iterable}

Isn't that second form the exact thing you were looking for, back in
your first post?

> I'm sorry you think the current edition of Google does such a fine job.
> Has it really been that many years ago that the results Google returned
> from a  properly formatted boolean search were really useful? I'd
> imagine that the old Google would have returned a good 10 pages or more
> (probably a lot more) of urls containing the phrase "dict comprehension"
> or "dictionary comprehension". in which you'd find a rich variety of
> specific situations to glean through. (You wouldn't have needed to
> include "python" in the search phrase, since no other programming
> language that I know of, or other English usage for that matter, has
> dict comprehensions.)

Is this not your experience today?  I just browsed through the first 5
pages of search results for the phrase "dict comprehension" (without
the quotes), and the results seem to be pretty on point.  It's mostly
results talking about python dict comprehensions, general python pages
talking about all sorts of comprehensions (dict, list, and set), and
as you get deeper into the result pages, you start to see some entries
for the word "comprehension" in dictionaries too, which seems like a
reasonable thing to end up mixed in with the desired results.  It goes
on in that vein out to page 11 or so, at which point things seem to
devolve a bit.

I'd be totally sympathetic with your plight if you didn't know the key
phrase 'dict comprehension' to find all of that information.  I'm just
not seeing the poor results you seem to be getting from Google once
you know the term.

-- 
Jerry
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Larry Martell
On Mon, Apr 3, 2017 at 8:24 PM Dennis Lee Bieber 
wrote:

> On Mon, 3 Apr 2017 11:48:38 -0700, "Deborah Swanson"
>  declaimed the following:
>
> >But, if Larry Page and Sergey Brin could tinker around in their dorm
> >rooms (or wherever they lived then) and they made the first Google (the
> >first search engine?) to boolean search the World Wide Web, it shouldn't
> >be so awfully hard to make a collection of Python docs that's boolean
> >searchable.



> >>Alta Vista predated Google by a few years (it was later absorbed by
> Yahoo).


Don't  forget Ask Jeeves

>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-03 Thread Steve D'Aprano
On Tue, 4 Apr 2017 03:27 am, Deborah Swanson wrote:

> I'll admit that both dictionaries and comprehensions are still a little
> bit fuzzy to me, especially when I get away from the common usages. This
> could be a good exercise to clarify some of the fuzzy areas.


As far as comprehensions go, how is your mathematics?

If you remember your set builder notation from maths, list comprehensions
are based on that.

In maths, we say something like:

{2n + 1 : 0 ≤ n ≤ 9}

which might be read aloud as "the set of 2 times n, plus 1, such that n is
between 0 and 9 inclusive".

http://www.mathwords.com/s/set_builder_notation.htm

A similar notation might be:

{2n + 1 : n ∈ {1, 2, 3}}

said as "the set of 2 times n, plus 1, such that n is an element of the set
{1, 2, 3}".


If you're a maths geek like me, this is second nature :-)


Now, Python's list comprehension syntax is based on a similar notation,
except we spell it out in words rather than symbols, using the
familiar "for n in ..." syntax from for-loops instead of : "such that".

{2*n + 1 for n in range(10)}

{2*n + 1 for n in (1, 2, 3)}


A few other differences:

- list comprehensions use [ ] for the outermost brackets;
- set and dict comprehensions use { };
- generator expressions use ( ) (unless the parentheses can be implied).

We're not restricted to mathematical expressions, and can use any Python
expression we like:

[value.method(arg)[1] for (value, arg) in zip(values, arguments)]


translates roughly into this for-loop:

result = []
for (value, arg) in zip(values, arguments):
result.append(value.method(arg)[1])



Another difference is that comprehensions allow an "if" clause:

[v.method(a)[1] for (v, a) in zip(values, arguments) if v is not None]

translates something like:


result = []
for (v, a) in zip(values, arguments):
if v is not None:
result.append(v.method(a)[1])


There's more, but that covers probably 98% of comprehension usage.

And of course, remember that dict comprehensions use the same key:value
syntax as ordinary dict "literals" (the official name is "dict display").

result = {}
for key, value in zip(keys, values):
result[key] = value


becomes

result = {key:value for (key, value) in zip(keys, values)}

although frankly that's better written as:

dict(zip(keys, values))



Hope this helps!




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Gregory Ewing

Deborah Swanson wrote:

I'd
imagine that the old Google would have returned a good 10 pages or more
(probably a lot more) of urls containing the phrase "dict comprehension"
or "dictionary comprehension".


It still does, as far as I can see. I just googled for "dict
comprehension", and the vast majority of results in the first
10 pages relate to Python.

By page 20 it's starting to wander off a bit, but you can
hardly blame it for that. There *are* non-Python web pages that
mention the words "dict" and "comprehension", and how is Google
to know that you don't want those if you don't tell it?


You used to be able to keep sifting through pages of results
after the bulk of urls fitting your criteria had passed, and still find
useful things to look at, sometimes at page 500


Seems to me Google was doing a rather *bad* job if you had
to wade through 500 pages of results to find what you wanted.
I would never have the patience to do that!

Anyhow, the reason Google got brought up was that you were
complaining about difficulty of finding things in the Python
docs. Google *does* turn up the relevant part of the docs in
the very first page of results, so being able to do a direct
text search on the docs wouldn't do any better.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Gregory Ewing

Deborah Swanson wrote:


But, if Larry Page and Sergey Brin could tinker around in their dorm
rooms (or wherever they lived then) and they made the first Google (the
first search engine?)


It wasn't the first web search engine. But it was the first
one that not only worked, but *kept* working as the web
grew bigger, and bigger, and bigger.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Gregory Ewing

Deborah Swanson wrote:

All my guesses were based on the
single variable (the most common type) examples I found. I just didn't
think of putting a colon after 'label', and found nothing to suggest
that's what I should do.


Hmmm, I'm not sure what the docs could do to make that any
clearer. The key:value syntax is part of *every* dict
comprehension (it's the only thing that distinguishes a
dict comprehension from a set comprehension).


Despite my earlier words and protestations that I did look for two
variable dict comprehensions, fairly diligently, I am taking what you
said seriously.


Don't feel too bad. Sometimes it's hard to know what to google
for, even for experienced people! Also it's hard for doc
writers to anticipate how less experienced people will
think. It wouldn't have occurred to me to use the phrase
"two variable dict comprehension" when writing documentation.

Another thing it's important to be able to do is see through
to the principles behind things, and then use those principles
to solve new problems that aren't explicitly covered in any
docs or examples.

The general principle behind all the forms of comprehension
is that they consist of a prototypical element, followed by
some clauses that generate values for that element.

The syntax for the prototypical element mirrors that of the
corresponding display. So, for a dict comprehension, the
prototypical element has the form 'key:value', where 'key'
and 'value' are arbitrary expressions. From there, it's just
a matter of figuring out what to put into those expressions.

In your case, another part of the puzzle is the fact that
you can unpack a tuple obtained from a for-loop into
variables. That's a general feature of for-loops, both
inside and outside of comprehensions, so you probably
won't find it mentioned explicitly under the heading of
dict comprehensions.


Maybe it
would be worthwhile to scrape the whole mess and have it in searchable
text form.


The HTML Python docs have a "Search" box, although I haven't
really used it so I don't know how well it works. In my experience,
Google with a site search often works a lot better than
search functions provided by sites themselves.


I hope you won't be miffed though if I still come up empty handed and
come back here to ask again.

Deborah


--
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Gregory Ewing

Deborah Swanson wrote:


I'll admit that both dictionaries and comprehensions are still a little
bit fuzzy to me, especially when I get away from the common usages. This
could be a good exercise to clarify some of the fuzzy areas.


If you're fuzzy about dictionaries in general, it might be a
good idea to concentrate on that for now and come back to
comprehensions later. They're something of an advanced topic;
anything you can do with a comprehension can also be done
in more fundamental ways.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Nathan Ernst
No worries, Deborah.

Python is by most measurements a relatively easy/simple language to learn,
but there are always the dusty corners. If you've not already, I recommend
going through the online Python tutorial in it's entirety (
https://docs.python.org/3/tutorial/index.html).

After that, learn the language syntax that wasn't covered in the tutorial
by reading the Language Reference (
https://docs.python.org/3/reference/index.html). The tutorial should be
fairly easy for a straight beginner to follow. The language reference
assumes a little higher-level understanding of programming language
grammar.  The Python Language Reference uses a modified BNF syntax (BNF
being Backus-Naur form. You can read about BNF at
https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form). To be honest, I'm
not sure what modifications Python uses to BNF, maybe someone else can shed
some light (or skin) on it.

After you'd done those, peruse the standard library. I don't recommend deep
reading there at this point, but at least a cursory reading so you're
cognizant of libraries that are built-in that may help do things may you
want to do now, or in the future (i.e. make a web request, parse JSON or
XML, handle datetimes).

Remember: Python comes with batteries included.

-Nate

On Mon, Apr 3, 2017 at 5:09 PM, Deborah Swanson 
wrote:

> Nathan Ernst wrote, on April 03, 2017 1:59 PM
> >
> > I was a bit surprised when I looked at the language reference
> > for 3.6.x. I expected there'd be a direct link to
> > comprehensions, but there's not.
> >
> > You have to know what you're looking for:
> >
> > 6.2.5: List Displays
> > 6.2.6: Set Displays
> > 6.2.7: Dictionary Displays
> >
> > And, then, click on the appropriate element of the sub
> > grammar to find the appropriate syntax.
> >
> > So, it took me about 30 seconds to find the appropriate
> > grammars, when I expected it'd only take about 5 seconds,
> > since I'm very familiar with the python docs & how the
> > grammar documentation is laid out.  I can fully understand
> > how someone less familiar with the documentation might have a
> > harder time finding the grammar than I did.
> >
> > FWIW, If one was completely new to Python, even knowing the
> > syntax is known as a "comprehension" might be unknown. I
> > certainly didn't know what a comprehension was when I was
> > learning Python. A coworker showed me, some 13 years ago.
> >
> > Regards,
> > Nate
>
> Thanks Nate, for your comprehension of the plight of many, if not most,
> newish Python coders. And it certainly doesn't help to ask the list to
> fill in some of the holes and be met with criticism for asking, but I
> digress. It is what it is.
>
> Before I started reading the list a few months ago, I'd heard of list
> comprehensions in an article I'd read, and hardly understood the gist of
> it. But look at me now Ma, I've learned not only how to use list
> comprehensions but also a small tribe of other kinds of comprehensions!
>
> (If there's a moral to this story, heck if I know exactly what it is.
> "Keep on trying" is as good as any.)
>
> Deborah
>
> > On Mon, Apr 3, 2017 at 3:47 PM, Jerry Hill
> >  wrote:
> >
> > > On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson
> > >  wrote:
> > > > Regular updates as the docs are updated would be a good idea too.
> > > > It's obvious that today's Google isn't up to it, although
> > it occurs
> > > > to me that I haven't tried Google's site search on python.org.
> > >
> > > So, when you search google for the phrase "dict comprehension" or
> > > "dictionary comprehension", nothing useful comes up for
> > you?  When I
> > > search either of those phrases, I get lots of useful
> > results, all of
> > > which spell out how to do what you were originally asking about.  I
> > > know Google search results are skewed by past usage, but
> > I'm surprised
> > > that you didn't find anything useful in the first couple of search
> > > results.
> > >
> > > When I do a search for 'dict comprehension' I get a boxed result
> > > linking to PEP 274 as the first hit, then two Stack Overflow
> > > questions, both of which demonstrate how to do dictionary
> > > comprehensions.  Following that is another link to PEP 274,
> > a link to
> > > the Python docs on data structures (which does talk about dict
> > > comprehensions, but it's way down on the page), and then links to a
> > > bunch of tutorials.  If you had to judge based on my search
> > results,
> > > Google does a fine job of answering python questions, at least when
> > > you already know the key phrase to look for.
> > >
> > > --
> > > Jerry
> > > --
> > > https://mail.python.org/mailman/listinfo/python-list
> > >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-03 Thread Deborah Swanson
Nathan Ernst wrote, on April 03, 2017 1:59 PM
> 
> I was a bit surprised when I looked at the language reference 
> for 3.6.x. I expected there'd be a direct link to 
> comprehensions, but there's not.
> 
> You have to know what you're looking for:
> 
> 6.2.5: List Displays
> 6.2.6: Set Displays
> 6.2.7: Dictionary Displays
> 
> And, then, click on the appropriate element of the sub 
> grammar to find the appropriate syntax.
> 
> So, it took me about 30 seconds to find the appropriate 
> grammars, when I expected it'd only take about 5 seconds, 
> since I'm very familiar with the python docs & how the 
> grammar documentation is laid out.  I can fully understand 
> how someone less familiar with the documentation might have a 
> harder time finding the grammar than I did.
> 
> FWIW, If one was completely new to Python, even knowing the 
> syntax is known as a "comprehension" might be unknown. I 
> certainly didn't know what a comprehension was when I was 
> learning Python. A coworker showed me, some 13 years ago.
> 
> Regards,
> Nate

Thanks Nate, for your comprehension of the plight of many, if not most,
newish Python coders. And it certainly doesn't help to ask the list to
fill in some of the holes and be met with criticism for asking, but I
digress. It is what it is.

Before I started reading the list a few months ago, I'd heard of list
comprehensions in an article I'd read, and hardly understood the gist of
it. But look at me now Ma, I've learned not only how to use list
comprehensions but also a small tribe of other kinds of comprehensions!

(If there's a moral to this story, heck if I know exactly what it is.
"Keep on trying" is as good as any.)

Deborah

> On Mon, Apr 3, 2017 at 3:47 PM, Jerry Hill 
>  wrote:
> 
> > On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson 
> >  wrote:
> > > Regular updates as the docs are updated would be a good idea too. 
> > > It's obvious that today's Google isn't up to it, although 
> it occurs 
> > > to me that I haven't tried Google's site search on python.org.
> >
> > So, when you search google for the phrase "dict comprehension" or 
> > "dictionary comprehension", nothing useful comes up for 
> you?  When I 
> > search either of those phrases, I get lots of useful 
> results, all of 
> > which spell out how to do what you were originally asking about.  I 
> > know Google search results are skewed by past usage, but 
> I'm surprised 
> > that you didn't find anything useful in the first couple of search 
> > results.
> >
> > When I do a search for 'dict comprehension' I get a boxed result 
> > linking to PEP 274 as the first hit, then two Stack Overflow 
> > questions, both of which demonstrate how to do dictionary 
> > comprehensions.  Following that is another link to PEP 274, 
> a link to 
> > the Python docs on data structures (which does talk about dict 
> > comprehensions, but it's way down on the page), and then links to a 
> > bunch of tutorials.  If you had to judge based on my search 
> results, 
> > Google does a fine job of answering python questions, at least when 
> > you already know the key phrase to look for.
> >
> > --
> > Jerry
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-03 Thread Deborah Swanson
Jerry Hill wrote, on April 03, 2017 1:48 PM
> 
> On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson 
>  wrote:
> > Regular updates as the docs are updated would be a good 
> idea too. It's 
> > obvious that today's Google isn't up to it, although it 
> occurs to me 
> > that I haven't tried Google's site search on python.org.
> 
> So, when you search google for the phrase "dict 
> comprehension" or "dictionary comprehension", nothing useful 
> comes up for you?  When I search either of those phrases, I 
> get lots of useful results, all of which spell out how to do 
> what you were originally asking about.  I know Google search 
> results are skewed by past usage, but I'm surprised that you 
> didn't find anything useful in the first couple of search results.
> 
> When I do a search for 'dict comprehension' I get a boxed 
> result linking to PEP 274 as the first hit, then two Stack 
> Overflow questions, both of which demonstrate how to do 
> dictionary comprehensions.  Following that is another link to 
> PEP 274, a link to the Python docs on data structures (which 
> does talk about dict comprehensions, but it's way down on the 
> page), and then links to a bunch of tutorials.  If you had to 
> judge based on my search results, Google does a fine job of 
> answering python questions, at least when you already know 
> the key phrase to look for.
> 
> -- 
> Jerry

Ah, but did you actually try to use the proposed solutions on the two
stackoverflow pages? It's been several weeks now, but I did, and neither
of those two examples fit my situation, which is why I ended up writing
my own, and unsatisfactorily at that.

I'm sorry you think the current edition of Google does such a fine job.
Has it really been that many years ago that the results Google returned
from a  properly formatted boolean search were really useful? I'd
imagine that the old Google would have returned a good 10 pages or more
(probably a lot more) of urls containing the phrase "dict comprehension"
or "dictionary comprehension". in which you'd find a rich variety of
specific situations to glean through. (You wouldn't have needed to
include "python" in the search phrase, since no other programming
language that I know of, or other English usage for that matter, has
dict comprehensions.)

Nowadays Google just comes up with a mere handful of sorta appropriate
urls, and rarely do I find exactly what I'm looking for. And usually
there's nothing related to all your search terms after a page or two of
results. You used to be able to keep sifting through pages of results
after the bulk of urls fitting your criteria had passed, and still find
useful things to look at, sometimes at page 500 or even much, much
farther down the list. It really paid to comb through them all,
especially if you didn't find exactly what you wanted in the early
batch.

But giving users the choice among tens or hundreds of similar pages
(fewer if you specify 100 results per page) doesn't give Google as much
grist for the advertising mill to pump out at the users, hence the
present day useless mess.

Deborah

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Nathan Ernst
I was a bit surprised when I looked at the language reference for 3.6.x. I
expected there'd be a direct link to comprehensions, but there's not.

You have to know what you're looking for:

6.2.5: List Displays
6.2.6: Set Displays
6.2.7: Dictionary Displays

And, then, click on the appropriate element of the sub grammar to find the
appropriate syntax.

So, it took me about 30 seconds to find the appropriate grammars, when I
expected it'd only take about 5 seconds, since I'm very familiar with the
python docs & how the grammar documentation is laid out.  I can fully
understand how someone less familiar with the documentation might have a
harder time finding the grammar than I did.

FWIW, If one was completely new to Python, even knowing the syntax is known
as a "comprehension" might be unknown. I certainly didn't know what a
comprehension was when I was learning Python. A coworker showed me, some 13
years ago.

Regards,
Nate

On Mon, Apr 3, 2017 at 3:47 PM, Jerry Hill  wrote:

> On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson
>  wrote:
> > Regular updates as the docs are updated would be a good idea too. It's
> > obvious that today's Google isn't up to it, although it occurs to me
> > that I haven't tried Google's site search on python.org.
>
> So, when you search google for the phrase "dict comprehension" or
> "dictionary comprehension", nothing useful comes up for you?  When I
> search either of those phrases, I get lots of useful results, all of
> which spell out how to do what you were originally asking about.  I
> know Google search results are skewed by past usage, but I'm surprised
> that you didn't find anything useful in the first couple of search
> results.
>
> When I do a search for 'dict comprehension' I get a boxed result
> linking to PEP 274 as the first hit, then two Stack Overflow
> questions, both of which demonstrate how to do dictionary
> comprehensions.  Following that is another link to PEP 274, a link to
> the Python docs on data structures (which does talk about dict
> comprehensions, but it's way down on the page), and then links to a
> bunch of tutorials.  If you had to judge based on my search results,
> Google does a fine job of answering python questions, at least when
> you already know the key phrase to look for.
>
> --
> Jerry
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Jerry Hill
On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson
 wrote:
> Regular updates as the docs are updated would be a good idea too. It's
> obvious that today's Google isn't up to it, although it occurs to me
> that I haven't tried Google's site search on python.org.

So, when you search google for the phrase "dict comprehension" or
"dictionary comprehension", nothing useful comes up for you?  When I
search either of those phrases, I get lots of useful results, all of
which spell out how to do what you were originally asking about.  I
know Google search results are skewed by past usage, but I'm surprised
that you didn't find anything useful in the first couple of search
results.

When I do a search for 'dict comprehension' I get a boxed result
linking to PEP 274 as the first hit, then two Stack Overflow
questions, both of which demonstrate how to do dictionary
comprehensions.  Following that is another link to PEP 274, a link to
the Python docs on data structures (which does talk about dict
comprehensions, but it's way down on the page), and then links to a
bunch of tutorials.  If you had to judge based on my search results,
Google does a fine job of answering python questions, at least when
you already know the key phrase to look for.

-- 
Jerry
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-03 Thread Deborah Swanson
Rob Gaddi wrote, on April 03, 2017 10:38 AM
> 
> On 04/03/2017 10:27 AM, Deborah Swanson wrote:
> > Dennis Lee Bieber wrote, on April 03, 2017 9:35 AM
> >>
> >> On Mon, 3 Apr 2017 07:30:40 -0700, "Deborah Swanson" 
> >>  declaimed the following:
> >>
> >>>
> >>> Clearly there's more to be found in nooks, crannies and byways in
the
> >>> docs than you'll get to from the given pointers in the index.
Maybe it
> >>> would be worthwhile to scrape the whole mess and have it in
searchable
> >>> text form. Another thing Python would be the right tool for the
job 
> >>> for. Regular updates as the docs are updated would be a good idea
too.
> >>> It's obvious that today's Google isn't up to it, although it
occurs to
> >>> me that I haven't tried Google's site search on python.org.
> >>>
> >>On Windows, the (at least, for ActiveState releases)
documentation 
> >> is available in Windows Help format -- though I'll admit the "free 
> >> text search" leaves a lot to be desired...
> >>
> >>"dict comprehension" didn't find anything obvious; "dictionary 
> >> comprehension" brought up PEP 274 (note: I still use 2.7 as main 
> >> version).
> >>
> >> -=-=-=-=-=-
> >> Semantics
> >> The semantics of dict comprehensions can actually be
demonstrated
> >> in stock Python 2.2, by passing a list comprehension to the
> >> builtin dictionary constructor:
> >>
> >> >>> dict([(i, chr(65+i)) for i in range(4)])
> >>
> >> is semantically equivalent to
> >>
> >> >>> {i : chr(65+i) for i in range(4)}
> >>
> >> The dictionary constructor approach has two dictinct
disadvantages
> >> from the proposed syntax though.  First, it isn't as legible as
a
> >> dict comprehension.  Second, it forces the programmer to create
an
> >> in-core list object first, which could be expensive.
> >>
> >> -=-=-=-=-=-
> >> --
> >>Wulfraed Dennis Lee Bieber AF6VN
> >> wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/
> >
> > It would be interesting to play around with different list 
> > comprehensions than the one they've shown.
> >
> > I'll admit that both dictionaries and comprehensions are still a 
> > little bit fuzzy to me, especially when I get away from the common 
> > usages. This could be a good exercise to clarify some of the fuzzy 
> > areas.
> >
> > Deborah
> >
> 
> And don't forget
>dict_comp : { k:v for (k,v) in kv_provider }
>set_comp : { item for item in item_provider }
>set_of_tuples_comp : { (k,v) for (k,v) in kv_provider }
> 
> Just to make things more complicated.
> 
> -- 
> Rob Gaddi, Highland Technology -- www.highlandtechnology.com 
> Email address domain is currently out of order.  See above to fix.

Added to my list of comprehension types, thank you.

I'm thinking it would also be good to include the PEPs in my searchable
Python reference. Just making the collection and browsing through it
would be highly instructive.

But, if Larry Page and Sergey Brin could tinker around in their dorm
rooms (or wherever they lived then) and they made the first Google (the
first search engine?) to boolean search the World Wide Web, it shouldn't
be so awfully hard to make a collection of Python docs that's boolean
searchable. 

I haven't seen a search method for text that comes anywhere near boolean
search's completeness in returning results (though I'm happy to take
suggestions).

Deborah


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Rob Gaddi

On 04/03/2017 10:27 AM, Deborah Swanson wrote:

Dennis Lee Bieber wrote, on April 03, 2017 9:35 AM


On Mon, 3 Apr 2017 07:30:40 -0700, "Deborah Swanson"
 declaimed the following:



Clearly there's more to be found in nooks, crannies and

byways in the

docs than you'll get to from the given pointers in the

index. Maybe it

would be worthwhile to scrape the whole mess and have it in

searchable

text form. Another thing Python would be the right tool for the job
for. Regular updates as the docs are updated would be a good

idea too.

It's obvious that today's Google isn't up to it, although it

occurs to

me that I haven't tried Google's site search on python.org.


On Windows, the (at least, for ActiveState releases)
documentation is available in Windows Help format -- though
I'll admit the "free text search" leaves a lot to be desired...

"dict comprehension" didn't find anything obvious;
"dictionary comprehension" brought up PEP 274 (note: I still
use 2.7 as main version).

-=-=-=-=-=-
Semantics
The semantics of dict comprehensions can actually be demonstrated
in stock Python 2.2, by passing a list comprehension to the
builtin dictionary constructor:

>>> dict([(i, chr(65+i)) for i in range(4)])

is semantically equivalent to

>>> {i : chr(65+i) for i in range(4)}

The dictionary constructor approach has two dictinct disadvantages
from the proposed syntax though.  First, it isn't as legible as a
dict comprehension.  Second, it forces the programmer to create an
in-core list object first, which could be expensive.

-=-=-=-=-=-
--
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/


It would be interesting to play around with different list
comprehensions than the one they've shown.

I'll admit that both dictionaries and comprehensions are still a little
bit fuzzy to me, especially when I get away from the common usages. This
could be a good exercise to clarify some of the fuzzy areas.

Deborah



And don't forget
  dict_comp : { k:v for (k,v) in kv_provider }
  set_comp : { item for item in item_provider }
  set_of_tuples_comp : { (k,v) for (k,v) in kv_provider }

Just to make things more complicated.

--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
--
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-03 Thread Deborah Swanson
Dennis Lee Bieber wrote, on April 03, 2017 9:35 AM
> 
> On Mon, 3 Apr 2017 07:30:40 -0700, "Deborah Swanson" 
>  declaimed the following:
> 
> >
> >Clearly there's more to be found in nooks, crannies and 
> byways in the 
> >docs than you'll get to from the given pointers in the 
> index. Maybe it 
> >would be worthwhile to scrape the whole mess and have it in 
> searchable 
> >text form. Another thing Python would be the right tool for the job 
> >for. Regular updates as the docs are updated would be a good 
> idea too. 
> >It's obvious that today's Google isn't up to it, although it 
> occurs to 
> >me that I haven't tried Google's site search on python.org.
> >
>   On Windows, the (at least, for ActiveState releases) 
> documentation is available in Windows Help format -- though 
> I'll admit the "free text search" leaves a lot to be desired...
> 
>   "dict comprehension" didn't find anything obvious; 
> "dictionary comprehension" brought up PEP 274 (note: I still 
> use 2.7 as main version).
> 
> -=-=-=-=-=-
> Semantics
> The semantics of dict comprehensions can actually be demonstrated
> in stock Python 2.2, by passing a list comprehension to the
> builtin dictionary constructor:
> 
> >>> dict([(i, chr(65+i)) for i in range(4)])
> 
> is semantically equivalent to
> 
> >>> {i : chr(65+i) for i in range(4)}
> 
> The dictionary constructor approach has two dictinct disadvantages
> from the proposed syntax though.  First, it isn't as legible as a
> dict comprehension.  Second, it forces the programmer to create an
> in-core list object first, which could be expensive.
> 
> -=-=-=-=-=-
> -- 
>   Wulfraed Dennis Lee Bieber AF6VN
> wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/

It would be interesting to play around with different list
comprehensions than the one they've shown.

I'll admit that both dictionaries and comprehensions are still a little
bit fuzzy to me, especially when I get away from the common usages. This
could be a good exercise to clarify some of the fuzzy areas.

Deborah

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-03 Thread Deborah Swanson
Gregory Ewing wrote, on April 02, 2017 11:35 PM
> 
> Deborah Swanson wrote:
> 
> > Oh, come on. That's a fairly obscure citation in the docs, one that 
> > would take a good deal of experience and time reading 
> through them to 
> > know was there,
> 
> You seemed to know that there was something called a "dict 
> comprehension". Googling for "python 3 dict comprehension" 
> gives me a link to an example of one in the docs as the second result.
> 
> The first result is a page in Dive Into Python containing
> a section on dict comprehensions.
> 
> Part of being a good programmer is knowing how to track
> down the information you need!
> 
> Having said that, the index of the Python docs could be 
> improved a bit in this area -- currently it only mentions 
> "list" under "comprehension" (although the page it leads to 
> discusses the other types as well).
> 
> -- 
> Greg

Despite my earlier words and protestations that I did look for two
variable dict comprehensions, fairly diligently, I am taking what you
said seriously. Obviously I shouldn't expect to find handy and
recognizable entries in the index for everything I might want to find in
the docs, and I plan to spend more time browsing around. 

Clearly there's more to be found in nooks, crannies and byways in the
docs than you'll get to from the given pointers in the index. Maybe it
would be worthwhile to scrape the whole mess and have it in searchable
text form. Another thing Python would be the right tool for the job for.
Regular updates as the docs are updated would be a good idea too. It's
obvious that today's Google isn't up to it, although it occurs to me
that I haven't tried Google's site search on python.org.

I hope you won't be miffed though if I still come up empty handed and
come back here to ask again.

Deborah

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Chris Green
Gregory Ewing  wrote:
> 
> Part of being a good programmer is knowing how to track
> down the information you need!
> 
A very *large* part of it!  :-)


> Having said that, the index of the Python docs could be
> improved a bit in this area -- currently it only mentions
> "list" under "comprehension" (although the page it leads
> to discusses the other types as well).
> 
It's an area I tend to have trouble with, there are rather more
'lists' in Python than I find easy to remember all about.  I'm sure if
I was a full-time programmer in Python (I used to earn my living
writing C) I'd have it all at my finger tips.  Hoever, given that I'm
just a 'recreational' programmer in Python, and I'm getting old and
forgetful, I need something easy to refer to.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-03 Thread Gregory Ewing

Deborah Swanson wrote:


Oh, come on. That's a fairly obscure citation in the docs, one that
would take a good deal of experience and time reading through them to
know was there,


You seemed to know that there was something called a "dict
comprehension". Googling for "python 3 dict comprehension"
gives me a link to an example of one in the docs as the
second result.

The first result is a page in Dive Into Python containing
a section on dict comprehensions.

Part of being a good programmer is knowing how to track
down the information you need!

Having said that, the index of the Python docs could be
improved a bit in this area -- currently it only mentions
"list" under "comprehension" (although the page it leads
to discusses the other types as well).

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-02 Thread Deborah Swanson
Ben Finney wrote. on April 02, 2017 7:41 PM
> 
> "Deborah Swanson"  writes:
> 
> > Chris Angelico wrote, on April 02, 2017 6:37 PM
> > > 
> > > On Mon, Apr 3, 2017 at 11:26 AM, Deborah Swanson
> > > > Maybe I'm having another "dumb day" [.]
> >
> > Well, wouldncha know it, I never tried using a colon. That's what I 
> > get for just trying to guess.
> 
> Yes, guessing *is* dumb when the reference documentation is 
> available 
>  lays-for-lists-sets-and-dictionaries>.
> I was sure you'd have been reading the manual before 
> guessing, but I'll know better in future :-)
> 
> -- 
>  \   "A computer once beat me at chess, but it was no 
> match for me |
>   `\ at kick boxing." 
> -Emo Philips |
> _o__) 
>  |
> Ben Finney
> 

Oh, come on. That's a fairly obscure citation in the docs, one that
would take a good deal of experience and time reading through them to
know was there, experience and years with Python that I don't have. But
you knew that ... ;)

Deborah

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-02 Thread Ben Finney
"Deborah Swanson"  writes:

> Chris Angelico wrote, on April 02, 2017 6:37 PM
> > 
> > On Mon, Apr 3, 2017 at 11:26 AM, Deborah Swanson 
> > > Maybe I'm having another "dumb day" […]
>
> Well, wouldncha know it, I never tried using a colon. That's what I
> get for just trying to guess.

Yes, guessing *is* dumb when the reference documentation is available
.
I was sure you'd have been reading the manual before guessing, but I'll
know better in future :-)

-- 
 \   “A computer once beat me at chess, but it was no match for me |
  `\ at kick boxing.” —Emo Philips |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-02 Thread Deborah Swanson
Ben Finney wrote, on April 02, 2017 6:38 PM
> 
> "Deborah Swanson"  writes:
> 
> > It seems like this should be easy to rewrite as a dict 
> comprehension:
> >
> > records_idx = {}
> > for idx, label in enumerate(records[0]):
> > records_idx[label] = idx
> 
> How about this::
> 
> records_idx = {
> label: idx
> for (idx, label) in enumerate(collection_of_labels)
> }
> 
> You may have tripped on the ambiguity of the comma in its 
> surrounding context. I always prefer to put parens around the 
> items I intend to be comma-separated, to remove that ambiguity.
> 
> -- 
>  \"[It's] best to confuse only one issue at a time." 
> -Brian W. |
>   `\  Kernighan, Dennis M. Ritchie, _The C programming 
> language_, 1988 |
> _o__) 
>  |
> Ben Finney

That's a neat way to think about the problem. Me, I was guessing all the
way, and pleasantly surprised when the 3-statement concoction I came up
with worked.

Thanks Ben!

Deborah

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Two variable dictionary comprehension

2017-04-02 Thread Deborah Swanson
Chris Angelico wrote, on April 02, 2017 6:37 PM
> 
> On Mon, Apr 3, 2017 at 11:26 AM, Deborah Swanson 
>  wrote:
> > It seems like this should be easy to rewrite as a dict 
> comprehension:
> >
> > records_idx = {}
> > for idx, label in enumerate(records[0]):
> > records_idx[label] = idx
> >
> > Maybe I'm having another "dumb day", or maybe I've just been 
> > struggling with this (larger) problem for too long, but eveything I 
> > try to get an equivalent dict comprehension for the 3 lines above is

> > not working.
> 
> Should be this:
> 
> records_idx = {label: idx for idx, label in enumerate(records[0])}
> 
> That's a direct translation of your code.
> 
> ChrisA

Well, wouldncha know it, I never tried using a colon. That's what I get
for just trying to guess.

Thanks Chris

Deborah

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-02 Thread Ben Finney
"Deborah Swanson"  writes:

> It seems like this should be easy to rewrite as a dict comprehension:
>
> records_idx = {}
> for idx, label in enumerate(records[0]):
> records_idx[label] = idx

How about this::

records_idx = {
label: idx
for (idx, label) in enumerate(collection_of_labels)
}

You may have tripped on the ambiguity of the comma in its surrounding
context. I always prefer to put parens around the items I intend to be
comma-separated, to remove that ambiguity.

-- 
 \“[It's] best to confuse only one issue at a time.” —Brian W. |
  `\  Kernighan, Dennis M. Ritchie, _The C programming language_, 1988 |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two variable dictionary comprehension

2017-04-02 Thread Chris Angelico
On Mon, Apr 3, 2017 at 11:26 AM, Deborah Swanson
 wrote:
> It seems like this should be easy to rewrite as a dict comprehension:
>
> records_idx = {}
> for idx, label in enumerate(records[0]):
> records_idx[label] = idx
>
> Maybe I'm having another "dumb day", or maybe I've just been struggling
> with this (larger) problem for too long, but eveything I try to get an
> equivalent dict comprehension for the 3 lines above is not working.

Should be this:

records_idx = {label: idx for idx, label in enumerate(records[0])}

That's a direct translation of your code.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Two variable dictionary comprehension

2017-04-02 Thread Deborah Swanson
It seems like this should be easy to rewrite as a dict comprehension:

records_idx = {}
for idx, label in enumerate(records[0]):
records_idx[label] = idx

Maybe I'm having another "dumb day", or maybe I've just been struggling
with this (larger) problem for too long, but eveything I try to get an
equivalent dict comprehension for the 3 lines above is not working.

-- 
https://mail.python.org/mailman/listinfo/python-list