Code Explaination: Spelling correction code
Steven Bethard
steven.bethard at gmail.com
Thu Apr 12 10:28:04 EDT 2007
Drew wrote:
> On Apr 11, 11:27 pm, Steven Bethard <steven.beth... at gmail.com> wrote:
>> Drew wrote:
>>> def known_edits2(word):
>>> return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
>>> NWORDS)
>>
>> This is the same as:
>>
>> result = set()
>> for e1 in edits1(word):
>> for e2 in edits1(e1):
>> if e2 in NWORDS:
>> result.add(e2)
>> return result
>>
>> The thing between the ``set(`` and ``)`` is called a generator
>> comprehension if you'd like to look into it further.
>
> Thanks for the response. I'm somewhat familiar with generator/list
> comprehension but was unsure how multiple statements were evaluated
> when chained together. From your explanation, I'm assuming they are
> evaluated from the "inside out" rather than left to right or right to
> left.
>
> Does the mean that the comprehension on the inside is always evaluated
> first?
Not really (at least for the most literal interpretation of ``evaluated
first``). I find it easiest to think of translating them into regular
for loops by adding the appropriate indentation.
Starting with:
(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)
Adding newlines:
(e2
for e1 in edits1(word)
for e2 in edits1(e1)
if e2 in NWORDS)
Adding indentation:
(e2
for e1 in edits1(word)
for e2 in edits1(e1)
if e2 in NWORDS)
Moving the add/append to the bottom:
for e1 in edits1(word)
for e2 in edits1(e1)
if e2 in NWORDS
e2
Adding the remaining boiler-plate:
result = set()
for e1 in edits1(word):
for e2 in edits1(e1):
if e2 in NWORDS:
result.add(e2)
So multiple for- and if-expressions are evaluated in the same order that
they would normally be in Python, assuming the proper whitespace was added.
HTH,
STeVe
More information about the Python-list
mailing list