By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Couple functions I need, assuming they exist?

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Charles Krug wrote:
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
Jul 19 '05 #2

P: n/a
Charles Krug wrote:
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
Jul 19 '05 #3

P: n/a
Peter Hansen wrote:
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.

import locale
locale.setlocale(locale.LC_ALL, '') 'English_United Kingdom.1252' print locale.format("%d", 1000000, True) 1,000,000 print locale.format("%.2f", 1000000, True) 1,000,000.00 locale.setlocale(locale.LC_ALL, 'fr') 'French_France.1252' print locale.format("%d", 1000000, True) 1*000*000 print locale.format("%.2f", 1000000, True)

1*000*000,00

Jul 19 '05 #4

P: n/a
> 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.
num2str(65837) '65,837' num2str(6582942) '6,582,942' num2str(23) '23' num2str(-1934)

'-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

Jul 19 '05 #5

P: n/a

"Charles Krug" <cd****@worldnet.att.net> wrote in message
news:TV*********************@bgtnsc04-news.ops.worldnet.att.net...

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.
Jul 19 '05 #6

P: n/a
On 20 Jun 2005 15:51:07 GMT, Duncan Booth <du**********@invalid.invalid> wrote:
Peter Hansen wrote:
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.

import locale
locale.setlocale(locale.LC_ALL, '') 'English_United Kingdom.1252' print locale.format("%d", 1000000, True)

1,000,000


Perfect!

Thanks.

Sometimes "hard part" is figuring out which package already does the
thing I need done.
Charles.
Jul 19 '05 #7

P: n/a
Charles Krug <cd****@worldnet.att.net> writes:

[snip]
The target of the problems (my daughter) ...

[snip]

That sounds familiar :-). See:
http://www.seanet.com/~hgg9140/math/index.html
http://www.seanet.com/~hgg9140/math/k6.html
--
ha************@boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 294-4718
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.