[Tutor] list comprehension with else

Sydney Shall s.shall at virginmedia.com
Sun Jan 18 14:37:47 CET 2015

On 18/01/2015 13:20, Sydney Shall wrote:
> I am a beginner and I have a question of syntax.
> I am just learning to use list comprehension, which oc course, I find
> very helpful indeed.
> However, I am stuck with a specific problem of how to incorporate an
> else in a list comp-rehension. I cannot do it.
> The following snippet of code does what I need. But, perhaps I am
> solving this math error problem incorrectly.
> The problem is I am occasionally getting exactly zeros when I need to
> obtain the logarithm of the number.
> for i in range(len(cap)):
>          if cap[i] == 0.0:
>              tmp = math.log(1.0)
>              lncap.append(tmp)
>          else:
>              lncap.append(math.log(cap[i]))
> I have set mu options to plain text.

I apoligise. I should have added:
MAC OS OSX 10.9.5
python 2.7.6 (Enthought)


More information about the Tutor mailing list