Python Forum
How to exclude characters when counting digits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to exclude characters when counting digits
#15
digit_re = re.compile('\d+')

def re_count(text):
    return len(text) - len(digit_re.sub(text, ''))
Output:
count_func took 7.043 sec smart_count_digits took 0.945 sec ick_count took 0.622 sec re_count took 0.061 sec
Edit: Function gave wrong answer, corrected function and reran times.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: How to exclude characters when counting digits - by ichabod801 - Jul-03-2018, 01:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Counting the number of occurrences of characters in a string nsadams87xx 1 1,970 Jun-16-2020, 07:22 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020