I feel stoopid ... this code is to verbose
Joseph Wilhelm
jwilhelm at outsourcefinancial.com
Fri Jun 14 13:07:32 EDT 2002
On Fri, 2002-06-14 at 09:19, Max M wrote:
<snip>
> def stringSlicer(string, chunkSize=3):
> chunkList = []
> reverseString = list(string)
> reverseString.reverse()
> for i in range(0, len(string), chunkSize):
> chunk = reverseString[i:i+chunkSize]
> chunk.reverse()
> chunk = ''.join(chunk)
> chunkList.append(chunk)
> chunkList.reverse()
> return '.'.join(chunkList)
>
>
> print stringSlicer('42000000')
>
> >>> 40.000.000
>
> I just find that it's a lot of code for such a little function an it
> annoys my sense of aestetics. I have tried a lot of different approaches
> including using zip on a list like ['','','.'], and other weird stuff :-)
>
> I just cannot seem to find the nice 3-liner I expected from the
> beginning. Has anybody got a better solution ? I thought somebody might
> find it a fun exercise. Well I have...
<snip>
How about this:
import string
def stringSlicer( str, chunkSize=3 ):
parts = list( str )
for x in range( ( len( parts ) - chunkSize ), 0, -chunkSize ):
parts.insert( x, "." )
return string.join( parts, "" )
print stringSlicer( '42000000' )
>>> 42.000.000
--Joseph Wilhelm
More information about the Python-list
mailing list