[Tutor] New to this list ....

Asokan Pichai pasokan at talentsprint.com
Fri Mar 30 18:03:01 CEST 2012


On Fri, Mar 30, 2012 at 9:12 PM, Barry Drake <bdrake at crosswire.org> wrote:
> On 30/03/12 16:19, Evert Rol wrote:
>>
>> Not sure. In the sense that you can "optimise" (refactor) it in the same
>> way you could do with C. Eg:
>> results = [0, 0, 0]
>> flags = [0, 1, 2, 3]
>> for flag in flags:
>>     results = getflag(flag, results)
>>
>
> That's exactly what I hoped for.  I hadn't realised I can initialise a list
> in one go - it seems that lists work a lot like the arrays I was used to in
> c.  Thanks to the others who took the time to answer.  Just now, Asokan's
> solution is a bit obscure to me - I'll work on that one, but the above is
> lovely and elegant; and easy to understand.  Someone asked about the getflag
> function - it is:
>

> def getflag(thisflag, results):
>    if (thisflag == 2):
>        results[0] += 1
>    elif (thisflag == 1):> In c, I would have used switch and case, but I gather there is no direct
> equivalent in Python ...   But it works as is.
>
> --
> From Barry Drake - a member of the Ubuntu advertising team.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

and directly index on thisflag
>        results[1] += 1
>    elif (thisflag == 0):
>        results[2] += 1
>    return(results)
>
For the specific values it may be simpler to write:

def getflag(thisflag, results):
     results[2- thisflag] += 1
     return results

Or you may rearrange the meaning of results
and write

     results[thisflag] += 1

HTH
Asokan Pichai


More information about the Tutor mailing list