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 7 1405
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
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
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
> 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
"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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dimension7 |
last post by:
All,
I am comparing to functions to see which is "better". In better, I mean more
efficient, optimize, faster, etc.
I have read other posts from other boards, but I'm not really sure of the...
|
by: David MacQuigg |
last post by:
I'm not getting any feedback on the most important benefit in my
proposed "Ideas for Python 3" thread - the unification of methods and
functions. Perhaps it was buried among too many other less...
|
by: Maximus |
last post by:
Hi,
I was just wondering, is it good to use return without arguments in a void
function as following:
void SetMapLayer()
{
if( !Map ) return;
layer = LAYER_MAP;
}
|
by: Anthony Jones |
last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking
to switch to C++. We are trying to write a few simple examples to
demonstrate the power of the language to our manager,...
|
by: Rob Somers |
last post by:
Hey people,
I read a good thread on here regarding the reason why we use function
prototypes, and it answered most of my questions, but I wanted to
double check on a couple of things, as I am...
|
by: Melkor Ainur |
last post by:
Hello,
I'm attempting to build an interpreter for a pascal-like language.
Currently, I don't generate any assembly. Instead, I just build an
abstract syntax tree representing what I've parsed...
|
by: Tim Conner |
last post by:
Hi,
Thanks to Peter, Chris and Steven who answered my previous answer about
regex to split a string. Actually, it was as easy as create a regex with the
pattern "/*-+()," and most of my string...
|
by: rehevkor5 |
last post by:
I am trying to use the reflection API in PHP 5 to execute the functions
in a class which looks like:
class Meow
{
function context()
{
function a()
{
}
|
by: v4vijayakumar |
last post by:
Why we need "virtual private member functions"? Why it is not an
(compile time) error?
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| | |