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
#14
(Jul-03-2018, 11:27 AM)gruntfutuk Wrote:
def count_func(s):
    return sum(c.isdigit() for c in s)
would be pretty efficient, but I didn't run time it.

I like this solution also, but I can't test it.
My CPU Speed is not fixed, so I get not usable results.

I guess the methodcall isdigit() takes bit time.
Maybe this can be faster:

import string


def count_func(s):
    return sum(1 for c in s if c in set(string.digits))
But again, I can't benchmark it :-/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: How to exclude characters when counting digits - by DeaD_EyE - Jul-03-2018, 01:13 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