1

I am trying to use the filter() out numbers in a string:

s = "The Great Depression lasted from 1929 to 1939."

numbers = filter(lambda n: n==isinstance(s,int),s)

for i in numbers:
    print(i)

however, when I print numbers nothing is printed- any ideas why this is? Is my lambda function correct?

if fhhf
  • 119
  • 6

1 Answers1

0

If you want it to print, you need to add list() to created a printable object. Otherwise, filter is only creating an abject, but it won't print.

s = "The Great Depression lasted from 1929 to 1939."

numbers = list(filter(lambda n: n.isdigit(),s))
print(numbers)

Or try this:

s = "The Great Depression lasted from 1929 to 1939."

numbers = list(filter(lambda n: n.isdigit(),s))

for i in numbers:
    print(int(i), end='')
Robin Sage
  • 969
  • 1
  • 8
  • 24