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
#19
After a lecture recently delivered in another forum about the evilness of the non-functional ways Hand , I started playing around with map, filter and reduce (I was using them occasionally before, but I decided to raise my level of involvement)

So, adding map to @ichabod801's solution should have brought salvation Pray ? Apparently not Confused

Output:
In [4]: def ick_count(text): ...: return sum(text.count(digit) for digit in string.digits) ...: ...: def ick_map_count(text): ...: return sum(map(text.count, string.digits)) ...: ...: In [5]: %timeit ick_count(my_string) 423 µs ± 6.69 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) In [6]: %timeit ick_map_count(my_string) 423 µs ± 10.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Counting the number of occurrences of characters in a string nsadams87xx 1 1,968 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