Couple functions I need, assuming they exist?

C

Charles Krug

List:

First, I'm reading that aString.split() is depreciated. What's the
current best practice for this?

Or am I mistaking that:

myWords = split(aString, aChar)

is depreciated but

myWords = aString.split(aChgar)

is not?

Second question, I've written a script that generates a LaTeX source
containing randomly generated arithmetic problems of various types.

The target of the problems (my daughter) would prefer that the thousands
be delimited. Is there a string function that does this?

Thanks


Charles
 
P

Peter Hansen

Charles said:
First, I'm reading that aString.split() is depreciated. What's the
current best practice for this?

Or am I mistaking that:

myWords = split(aString, aChar)
> is depreciated but

If you mean "import string; string.split(aString, aChar)" then
yes, it's deprecated (not "depreciated", by the way).
myWords = aString.split(aChgar)
is not?

Correct, this is perfectly acceptable.
Second question, I've written a script that generates a LaTeX source
containing randomly generated arithmetic problems of various types.

The target of the problems (my daughter) would prefer that the thousands
be delimited. Is there a string function that does this?

You refer to something like putting a comma between groups of three
digits, as in 1,000? This is locale-specific, and there's a "locale"
module that should have what you need.

-Peter
 
S

Steven Bethard

Charles said:
myWords = split(aString, aChar)

is depreciated but

myWords = aString.split(aChgar)

is not?

Yes, that's basically correct. What's deprecated are the functions in
the string module. So
string.split(a_str, b_str)
is deprecated in favor of
a_str.split(b_str)

The target of the problems (my daughter) would prefer that the thousands
be delimited. Is there a string function that does this?

I assume you mean translating something like '1000000' to '1,000,000'?
I don't know of an existing function that does this, but here's a
relatively simple implementation:

py> import itertools as it
py> def add_commas(s):
.... rev_chars = it.chain(s[::-1], it.repeat('', 2))
.... return ','.join(''.join(three_digits)
.... for three_digits
.... in it.izip(*[rev_chars]*3))[::-1]
....
py> add_commas('10')
'10'
py> add_commas('100')
'100'
py> add_commas('1000')
'1,000'
py> add_commas('1000000000')
'1,000,000,000'

In case you haven't seen it before, it.izip(*[itr]*N)) iterates over the
'itr' iterator in chunks of size N, discarding the last chunk if it is
less than size N. To avoid losing any digits, I initially pad the
sequence with two empty strings, guaranteeing that only empty strings
are discarded.

So basically, the function iterates over the string in reverse order, 3
characters at a time, and joins these chunks together with commas.

HTH,

STeVe
 
D

Duncan Booth

Peter said:
You refer to something like putting a comma between groups of three
digits, as in 1,000? This is locale-specific, and there's a "locale"
module that should have what you need.
1 000 000,00
 
G

George Sakkis

I assume you mean translating something like '1000000' to '1,000,000'?
I don't know of an existing function that does this, but here's a
relatively simple implementation:

py> import itertools as it
py> def add_commas(s):
... rev_chars = it.chain(s[::-1], it.repeat('', 2))
... return ','.join(''.join(three_digits)
... for three_digits
... in it.izip(*[rev_chars]*3))[::-1]
...

Or for an equivalent less cryptic (IMHO) recipe:

def num2str(num):
'''Return a string representation of a number with the thousands
being delimited.
'-1,934'
'''
parts = []
div = abs(num)
while True:
div,mod = divmod(div,1000)
parts.append(mod)
if not div:
if num < 0: parts[-1] *= -1
return ','.join(str(part) for part in reversed(parts))


Regards,
George
 
P

Paul Watson

Charles Krug said:
The target of the problems (my daughter) would prefer that the thousands
be delimited. Is there a string function that does this?

Be sure to use the locale approach and avoid rolling your own.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top