# How to filter all non-numeric characters ,and digits to make from string to numbers

arthurProject Source

I want to activate a function by following these steps:

1.First filter out all non-numeric characters.

2.Calculate any linear function on these numbers.

3.Calculate the product of each results and return an answer.

ex:

``````>>print(digit_com_mul("8?97D4b"))

36
``````

This is the function I tried to perform but I do not understand how after the filter to turn the string into numbers

``````def digit_complete_mul(data):
return reduce(lambda x,y:x*y,map(lambda x: 10-x,int(filter(lambda x:x.isdigit(),data))))
``````
python-3.xpython-3.4

answered 7 months ago Joe Iddon #1

With a string (`s`) and a linear function (`f`), the following works:

``````def func(s, f):
l = (f(int(c)) for c in s if c.isdigit())
p = 1
for i in l:
p *= i
return p
``````

You could also use the `functools.reduce` approach:

``````def func(s, f):
l = (f(int(c)) for c in s if c.isdigit())
return functools.reduce(lambda x,y : x*y, l)
``````

(you could throw that first line into the second if you wanted, but its slightly unnecessary)

And both give the expected output:

``````>>> func('a1b2cd3e', lambda x: x+1)
24
>>> 2 * 3 * 4 #<-- same calc.
24
``````

answered 7 months ago 9000 #2

Consider:

``````digits = (int(c) for c in input_string if c.isdigit())
values_of_f = map(f, digits)
product = functools.reduce(lambda acc, x: acc * x, values_of_f)
``````

Write down first, inline later (if ever).

answered 7 months ago arthurProject #3

So i find a way .

just in the linear function change the character to int:

``````from functools import reduce

def digit_complete_mul(data):
return reduce(lambda x,y:x*y,map(lambda x: 10-int(x),filter(lambda x:x.isdigit(),data)))
``````