Python Forum
Filter and str.isdigit producing an error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter and str.isdigit producing an error
#2
filter() returns a filter object, which is iterable. According to the documentation

https://docs.python.org/3/library/functions.html#filter

Quote:filter(function, iterable)
Construct an iterator 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 function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to the generator expression (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
You could do "".join(filter(str.isdigit, s)).
s = 'November 11 day' 
pp = int("".join(filter(str.isdigit, s)))
print(f" Digits only  -> {pp}")
tester_V likes this post
Reply


Messages In This Thread
RE: Filter and str.isdigit producing an error - by deanhystad - Aug-11-2022, 11:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyautogui.locateOnScreen producing error dude8074 6 3,966 Apr-17-2022, 05:05 PM
Last Post: bowlofred
  producing numbers out of a list bouraque7878 10 3,856 Nov-12-2021, 09:13 PM
Last Post: jefsummers
  Python isdigit() and None samtal 6 8,340 May-06-2021, 05:13 PM
Last Post: Gribouillis
  I don't think my program is producing the correct answer emmapaw24 1 1,769 Mar-31-2020, 01:13 AM
Last Post: stullis
  [Beginner] Code is not producing desired result fakej171 2 2,479 Mar-21-2020, 10:26 AM
Last Post: buran
  user input producing incorrect boolean al_Czervik 4 3,130 Mar-05-2020, 09:50 PM
Last Post: al_Czervik
  str.isdigit() vs str.isnumeric() Skaperen 4 6,366 Jun-14-2019, 01:54 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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