How can I tell when a string is in fact a number?
Joshua Muskovitz
josh at open.com
Sun Nov 5 18:52:12 EST 2000
Why use re at all?
def isanum(str):
import string
return reduce(lambda x, y: x and y in string.digits, str, str != "")
>>> isanum("")
0
>>> isanum("1234")
1
>>> isanum("123xxx566")
0
>>> isanum("123.45")
0
This correctly handles the original problem of ensuring that the input
string contains *only* digits. Of course, the original problem also
returned 1 for the empty input string (""). To handle this, replace [str !=
""] with [1] in the reduce function.
why-does-everyone-always-try-to-present-a-solution-to-something-other-than-t
he-original-problem'ly yrs,
-- josh
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
More information about the Python-list
mailing list