[Tutor] New to this list ....
Barry Drake
bdrake at crosswire.org
Fri Mar 30 17:42:52 CEST 2012
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):
results[1] += 1
elif (thisflag == 0):
results[2] += 1
return(results)
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.
More information about the Tutor
mailing list