function that counts...
John Posner
jjposner at optimum.net
Wed May 19 23:40:06 EDT 2010
On 5/19/2010 5:51 PM, Steven D'Aprano wrote:
> On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote:
>
>
> Rather than iterating over an index j = 0, 1, 2, ... and then fetching
> the jth character of the string, you can iterate over the characters
> directly. So the inner loop is better written:
>
> for c in s:
> sum += int(c)
>
Or, as a one-liner (and not shadowing the built-in *sum* function):
mysum = sum(map(int, str(i)))
-John
More information about the Python-list
mailing list