Recognize the amount of digits in a string

Thread Solved

Join Date: Jun 2009
Posts: 8
Reputation: Mclovin is an unknown quantity at this point 
Solved Threads: 0
Mclovin Mclovin is offline Offline
Newbie Poster

Recognize the amount of digits in a string

 
0
  #1
Jun 24th, 2009
I'm struggeling with a problem which probably is farely easy to solve. I just cant find a simple way to do it.

I want to be able to recognize the occurance of digits in a string. Lets say the string looks like:

  1. file_Name1 = 'abc123de'
  2. file_Name2 = 'qwe987rty'

Now I want to be able to pick out '123' and 987 from those strings. The numbers vary so I dont want to search for specific numbers, just find the digits..
Edit: In the end I want to be able to recognize the amount of digits (using len) but maybe there's a faster way to do this?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Recognize the amount of digits in a string

 
0
  #2
Jun 24th, 2009
I'd say split the string into a list and use the filter method:
  1. file_Name1 = 'abc123de'
  2. # store that string as a list of characters
  3. L1 = list(file_Name1)
  4. # call filter on it using a lambda expression
  5. L1 = filter(lambda x: x in '1234567890', L1)
  6. # L1 now = ['1', '2', '3']
  7. num_digits = len(L1)

EDIT:
If you need to know what filter does, lookie here:
  1. filter(function, iterable)
  2.  
  3. Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
  4.  
  5. Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None.
  6.  
  7. See itertools.filterfalse() for the complementary function that returns elements of iterable for which function returns false.
Last edited by shadwickman; Jun 24th, 2009 at 8:02 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 2
Reputation: anantwaykar is an unknown quantity at this point 
Solved Threads: 1
anantwaykar anantwaykar is offline Offline
Newbie Poster

Re: Recognize the amount of digits in a string

 
0
  #3
Jun 24th, 2009
you can compare the string character by charater
and then use the function which convert the string to int. If the result is true then that perticular character is number else
it is string.
Eg. in VB
String s1="abcd145df78"
int length=len(s1)
string ss=""
for i=0 to length-1
a=mid(s1,i,1)
if isnumeric(a) then
ss=ss & " " & a
next
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Recognize the amount of digits in a string

 
0
  #4
Jun 24th, 2009
Why did you post VB code? This is the Python forum. Regardless, please put it in [code][/code] tags.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 302
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 52
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Recognize the amount of digits in a string

 
0
  #5
Jun 24th, 2009
Shadwickmans solution using filter is a very good one!
Another way is to use regular expressions...

Without further ado, here's a simple example using regular expressions:

  1. #example using regular expressions
  2. import re
  3.  
  4. # here's the string we'll be searching:
  5. myStr = "abc123def"
  6.  
  7. # search through the string using a regular expression
  8. # this regex will return any decimal digits (0-9) it finds in the string
  9. temp = re.search('[0-9]+', myStr)
  10. # you could also use the following regex:
  11. # m = re.search('\d+', myStr)
  12. # which is another way of writing the above expression
  13.  
  14. # now lets process the results of the search...
  15. result = temp.group()
  16.  
  17. numDigits = len(result)
  18.  
  19. print "In the string:", myStr, "- There were", numDigits, "numerical digits found, the digits were", result

The code above is a little clunky and inelegant as we're storing variables we probably don't need to be..But it is only an example!

If you only wanted to know the number of digits in the string you could shorten the above example to 3 lines of code:
  1. # regular expressions example 2
  2. import re
  3.  
  4. myStr="abc123def"
  5.  
  6. print "Number of digits =", len( (re.search('[0-9]+', myStr)).group() )

As you can see, in the above code we're munging several operations into one command at the end of the print statement.

If you want to find out more about using regular expressions check out the documentation that ships with python (hit F1 in idle!) and search for 'regular expression' you'll find out all you need to know!

Cheers for now,
Jas.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 302
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 52
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Recognize the amount of digits in a string

 
0
  #6
Jun 24th, 2009
Sorry to continue a thread that's been marked solved, but I thought I'd better note that the solution I posted would only match a single block of numbers.
If we changed the value of myStr to "abc123def456", the examples I previously posted would only pick up the first block of digits (123).

In case you've got more than one block of digits in the string and you want to get them all you'd need to use findall() instead of search()....(See below)

  1. import re
  2.  
  3. myStr="abc123def456"
  4.  
  5. result = re.findall('[0-9]+', myStr)
  6.  
  7. numDigits = 0
  8. for i in result:
  9. numDigits+=len(i)
  10.  
  11. print "String:", myStr
  12. print "Digits found:", result
  13. print "Number of digits =", numDigits

Thought I'd better mention it!
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC