Repeated assignments - a more elegant approach (newbie)

ivannaz ivan.nazarenko at uol.com.br
Thu Jun 5 13:42:14 EDT 2003


Suppose I have the following code:

if a > 0:
    mna += a
    cnta += 1
if b > 0:
    mnb += b
    cntb += 1
if c > 0:
    mnc += c
    cntc += 1
... and so on, until 'z'

Is there a better way to write it? The idea would be something like
(which obviously does not work):

for (var, mean, count) in [(a, mna, cnta), (b, mnb, cntb), (c, mnc,
cntc)...]:
    if var > 0:
        mean += var
        count += 1

Regards,

Ivan




More information about the Python-list mailing list