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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to