[Tutor] Splitting a number into even- and odd- numbered digits
by Barry Carroll other posts by this author
Apr 19 2006 5:38PM messages near this date
Re: [Tutor] Use iterator to refer to an object's attribute?
|
Re: [Tutor] Splitting a number into even- and odd- numbered digits
Greetings:
I am writing a function that accepts a string of decimal digits, calculates a checksum and r
eturns it as a single character string.
The first step in the calculation is to split the input into two strings: the even- and odd-
numbered digits, respectively. The least significant digit is defined as odd.
The following code fragment does the job but seems sort of brutish and inelegant to me:
> >>>>>>
> >> s = '987654321'
> >> odd = ''
> >> for c in s[::-2]:
... odd = c + odd
...
> >> s = s[:-1]
> >> even = ''
> >> for c in s[::-2]:
... even = c + even
...
> >> odd
'97531'
> >> even
'8642'
> >>>>>>
Is there a better (i.e. more Pythonic) way to do this?
Thanks in advance for all your help.
Regards,
Â
Barry
barry.carroll@[...].com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.
-Quarry worker's creed
_______________________________________________
Tutor maillist - Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Barry Carroll
Python
Barry Carroll
Barry Carroll
John Fouhy
|