Re: 3 number and dot..

2007-11-02 Thread Steven D'Aprano
On Fri, 02 Nov 2007 16:39:12 +, Roberto Bonvallet wrote: On 31 oct, 22:21, Paul Rubin http://[EMAIL PROTECTED] wrote: def convert(n): assert type(n) in (int,long) I'd replace this line with n = int(n), more in the spirit of duck typing. Not necessarily. Something duck typing is

Re: 3 number and dot..

2007-11-02 Thread Roberto Bonvallet
On 31 oct, 22:21, Paul Rubin http://[EMAIL PROTECTED] wrote: def convert(n): assert type(n) in (int,long) I'd replace this line with n = int(n), more in the spirit of duck typing. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: 3 number and dot..

2007-11-02 Thread Roberto Bonvallet
On 2 nov, 14:54, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: Not necessarily. Something duck typing is too liberal in what it accepts. You might want convert(math.pi) to raise an exception What I meant was that the function should not reject unnecessarily non- numeric things

3 number and dot..

2007-10-31 Thread Abandoned
Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? -- http://mail.python.org/mailman/listinfo/python-list

Re: 3 number and dot..

2007-10-31 Thread Paul McNett
Abandoned wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? Assuming that the dots are always in the 3rd and 7th position in the string: def conv(s, sep=.): l = [s[0:3], s[3:6], s[6:]] return sep.join(l) print conv(12332321) -- pkm ~

Re: 3 number and dot..

2007-10-31 Thread Abandoned
On Oct 31, 10:18 pm, Paul McNett [EMAIL PROTECTED] wrote: Abandoned wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? Assuming that the dots are always in the 3rd and 7th position in the string: def conv(s, sep=.): l = [s[0:3], s[3:6], s[6:]]

Re: 3 number and dot..

2007-10-31 Thread Paul McGuire
On Oct 31, 2:58 pm, Abandoned [EMAIL PROTECTED] wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? x = (12332321,) while (x[0]0): x=divmod(x[0],1000)+x[1:] ... x (0, 12, 332, 321) ..join(map(str,x[1:])) '12.332.321' -- Paul --

Re: 3 number and dot..

2007-10-31 Thread Abandoned
On Oct 31, 10:38 pm, Paul McGuire [EMAIL PROTECTED] wrote: On Oct 31, 2:58 pm, Abandoned [EMAIL PROTECTED] wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? x = (12332321,) while (x[0]0): x=divmod(x[0],1000)+x[1:] ... x (0, 12, 332, 321)

Re: 3 number and dot..

2007-10-31 Thread Chris Mellon
On Oct 31, 2007 3:24 PM, Abandoned [EMAIL PROTECTED] wrote: On Oct 31, 10:18 pm, Paul McNett [EMAIL PROTECTED] wrote: Abandoned wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? Assuming that the dots are always in the 3rd and 7th position in

Re: 3 number and dot..

2007-10-31 Thread Roberto Bonvallet
On 31 oct, 16:58, Abandoned [EMAIL PROTECTED] wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? x = 12332321 '.'.join(''.join(i for n, i in g) for k, g in groupby(enumerate(reversed(str(x))), lambda (n, i): n//3))[::-1] '12.332.321' -- Roberto Bonvallet

Re: 3 number and dot..

2007-10-31 Thread Abandoned
On Oct 31, 10:38 pm, Paul McGuire [EMAIL PROTECTED] wrote: On Oct 31, 2:58 pm, Abandoned [EMAIL PROTECTED] wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? x = (12332321,) while (x[0]0): x=divmod(x[0],1000)+x[1:] ... x (0, 12, 332, 321)

Re: 3 number and dot..

2007-10-31 Thread elf
On Oct 31, 9:58 pm, Abandoned [EMAIL PROTECTED] wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? If you want do define your own function this will work, no matter how long the number is, or what separator you choose: def conv(s, sep='.'): start=len(s)%3

Re: 3 number and dot..

2007-10-31 Thread Abandoned
On Oct 31, 10:50 pm, Roberto Bonvallet [EMAIL PROTECTED] wrote: On 31 oct, 16:58, Abandoned [EMAIL PROTECTED] wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? x = 12332321 '.'.join(''.join(i for n, i in g) for k, g in

Re: 3 number and dot..

2007-10-31 Thread elf
On Oct 31, 9:58 pm, Abandoned [EMAIL PROTECTED] wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? Hi, If you want to define your own function, no matter what the length of the number is or what separator you want to choose, this will work: def conv(s,

Re: 3 number and dot..

2007-10-31 Thread Abandoned
On Oct 31, 10:50 pm, Roberto Bonvallet [EMAIL PROTECTED] wrote: On 31 oct, 16:58, Abandoned [EMAIL PROTECTED] wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? x = 12332321 '.'.join(''.join(i for n, i in g) for k, g in

Re: 3 number and dot..

2007-10-31 Thread Paul McNett
Abandoned wrote: On Oct 31, 10:18 pm, Paul McNett [EMAIL PROTECTED] wrote: Abandoned wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? Assuming that the dots are always in the 3rd and 7th position in the string: def conv(s, sep=.): l = [s[0:3],

Re: 3 number and dot..

2007-10-31 Thread Hrvoje Niksic
Abandoned [EMAIL PROTECTED] writes: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? I'm surprised that no one has proposed a regex solution, such as: import re re.sub(r'\d{1,3}(?=(?:\d{3})+$)', r'\g0.', str(1234567)) '1.234.567' --

Re: 3 number and dot..

2007-10-31 Thread Hrvoje Niksic
Paul McNett [EMAIL PROTECTED] writes: Chris Mellon has given you the best response: use the locale module for this. It may be the best choice as far as reuse is concerned, but it's not without serious drawbacks. For one, the locale model doesn't really allow forcing of the separators. Some

Re: 3 number and dot..

2007-10-31 Thread Steven D'Aprano
On Wed, 31 Oct 2007 21:39:05 +, Abandoned wrote: On Oct 31, 10:50 pm, Roberto Bonvallet [EMAIL PROTECTED] wrote: On 31 oct, 16:58, Abandoned [EMAIL PROTECTED] wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? x = 12332321 '.'.join(''.join(i

Re: 3 number and dot..

2007-10-31 Thread bearophileHUGS
Hrvoje Niksic: I'm surprised that no one has proposed a regex solution, such as: I presume many Python programmers aren't much used in using REs. import re re.sub(r'\d{1,3}(?=(?:\d{3})+$)', r'\g0.', str(1234567)) '1.234.567' It works with negative numbers too. It's a very nice solution

Re: 3 number and dot..

2007-10-31 Thread Paul Hankin
On Oct 31, 7:58 pm, Abandoned [EMAIL PROTECTED] wrote: Hi.. I want to do this: for examle: 12332321 == 12.332.321 How can i do? Short without being too unreadable: def conv(x, sep='.'): x = str(x)[::-1] return sep.join(x[i:i + 3] for i in range(0, len(x), 3))[::-1] Or more

Re: 3 number and dot..

2007-10-31 Thread George Sakkis
On Oct 31, 7:32 pm, [EMAIL PROTECTED] wrote: Hrvoje Niksic: I'm surprised that no one has proposed a regex solution, such as: I presume many Python programmers aren't much used in using REs. import re re.sub(r'\d{1,3}(?=(?:\d{3})+$)', r'\g0.', str(1234567)) '1.234.567' It works

Re: 3 number and dot..

2007-10-31 Thread Paul Rubin
Abandoned [EMAIL PROTECTED] writes: 12332321 == 12.332.321 Untested: def convert(n): assert type(n) in (int,long) if n 0: return '-%s'% convert(-n) if n 1000: return str(n) return '%s.%03d' % (convert(n//1000), n % 1000) -- http://mail.python.org/mailman/listinfo/python-list