[Tutor] list comprehension with else

Sydney Shall s.shall at virginmedia.com
Sun Jan 18 14:20:38 CET 2015


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.
-- 
Sydney


More information about the Tutor mailing list