0

For example: How can I write this function in lambda form?

a=1 #Example
def Number(a):
    try:
        float(a)
        bool_a = True
    except:
        bool_a = False
    return bool_a

I want the lambda function to be used with the map function on a list and check if the input is a number or not and add boolean values to another list.

L=[1,2,3,4,5,'','Example']
L2=list(map(Number,L)) #I want the Number function here to be in lambda form
print(L2)
Vexter
  • 5
  • 3
  • 2
    You can't do exception handling in a lambda function. – Klaus D. Dec 09 '22 at 05:53
  • @KlausD.So how can I use lambda and map to check if the members of a list are numbers or not? – Vexter Dec 09 '22 at 05:55
  • 1
    You don't, you use a normal function for that. – Klaus D. Dec 09 '22 at 05:55
  • I mean are you just checking if it is an int or not? because if so you could just use https://www.folkstalk.com/2022/10/python-check-if-number-is-float-or-int-with-code-examples.html#:~:text=be%20resolved%20successfully.-,How%20do%20you%20check%20if%20input%20is%20float%20or%20int,of%20classinfo%20and%20False%20otherwise. and then transform that into a lambda – Andrew Ryan Dec 09 '22 at 05:57
  • `L2 = list(map(lambda x: isinstance(x,int), L))` – Alexander Dec 09 '22 at 05:59
  • @AndrewRyan No, I want to check if the input is a number or not. Integer or float. I already saw this link you sent. – Vexter Dec 09 '22 at 06:01
  • So, `...lambda x: isinstance(x, int) or isinstance(x, float)...` – The Photon Dec 09 '22 at 06:13
  • 3
    If you want to use exception handling, which may be totally reasonable, you can't use a lambda expression. Here's the thing: **you don't need to**. Lambda expressions are a convenience, just use the function you already wrote. – juanpa.arrivillaga Dec 09 '22 at 06:21
  • Does this answer your question? [Python Try Catch Block inside lambda](https://stackoverflow.com/questions/12451531/python-try-catch-block-inside-lambda) – Woody1193 Dec 09 '22 at 06:50

2 Answers2

0

Well you can try this:

L = [1, 2, 3, 4, 5, '', 'Example', 4.5]
L2 = list(map(lambda x:str(x).replace(".","",1).isdigit(), L))
print(L2)

here we replaced the single (.) from the float and checked if the string is consist of digits. But the output will consist of only True/False. Use filter function if you need the edited list:

Input:

L = [1, 2, 3, 4, 5, '', 'Example', 4.5]
print(L)
L2 = list(map(lambda x:str(x).replace(".","",1).isdigit(), L))
print(L2)
L3 = list(filter(lambda x:str(x).replace(".","",1).isdigit(), L))
print(L3)

Output:

[1, 2, 3, 4, 5, '', 'Example', 4.5]
[True, True, True, True, True, False, False, True]
[1, 2, 3, 4, 5, 4.5]
0

You cannot insert exception handling into a lambda function because it has to be a single expression, which try-except logic is not. Therefore, you need to do your check in such a way that you can get the result you want without exception handling.

To my mind, you have four possible data conditions: an integer, a float, a numeric string or a non-numeric string. So, let's combine checks for these four conditions into a single expression:

Number = lambda x: isinstance(x, int) or isinstance(x, float) or (isinstance(x, str) and x.isnumeric())

After doing this, you can send it to map like so:

L = [1,2,3,4,5,'','Example']
L2 = list(map(Number, L))
print(L2)

That being said, you don't need a function to be a lambda to be able to send it to map. The code you have will work just fine, as written.

Woody1193
  • 7,252
  • 5
  • 40
  • 90