On 28Oct2014 21:33, Clayton Kirkwood <c...@godblessthe.us> wrote:
!From: Cameron Simpson [mailto:c...@zip.com.au]
!Let me try a less wordy diagram. You will need to be displaying in a
!constant width font :-)
!
! [ pair for pair in values if key == pair[0] ]
! ^^^^-- the expression that accrues in the resulting list
! ^^^^-- the loop variable, taken from the loop source values
! ^^^^^^-- the loop source values
! ^^^^^^^^^^^^^^-- condition for including the
! expression in the resulting list
!
!So that first "pair" could be any expression, it is almost only
!coincidence that it is the same as the loop variable. It is the same in
!this case because this is the idiomatic way to select particular values
!form an existing list.
!
!If we'd wanted the new list to contain double the original values we'd
!write:
!
! [ pair*2 for pair in values if key == pair[0] ]
Ok, I am somewhat confused again. In the original example up above, it
appears that the pair list or tuple gets overridden. In this one right
above, once again, the list gets overwritten again, but what is being
doubled?, each part of the tuple?
First up, this makes a list, not a tuple. (Hence the outermost [] instead of
(). No, there is not a tuple equivalenti:-)
So, to get things straight:
- values is a list of pairs (2-tuples)
- the list comprehension above creates a new list consisting the first
expression computed for each pair in "values" matching the condition
expression
And I had not noticed that "pair" was a 2-tuple. So "pair*2" is not a sensible
example. The point is that the first "pair" (or bad example "2*pair") is an
arbitrary expression, used to compute each value in the new list.
Cheers,
Cameron Simpson <c...@zip.com.au>
Don't have awk? Use this simple sh emulation:
#!/bin/sh
echo 'Awk bailing out!' >&2
exit 2
- Tom Horsley <tahors...@csd.harris.com>
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor