+ 1
a question about lambda
heres a coding. scores = [80,68,49,10,70,69,55] f = lambda a :True if a<60 else False fail = filter(f, scores) for item in fail: print(item) As I set lambda a, but "a" is useless outside the lambda function, why the coding still work?
5 odpowiedzi
+ 1
QTWizard I think I understand it. Thank you so much🙏🙏
0
a is an argument for f.
Like:
def f(a):
return True if a<60 else False.
0
QTWizard but when I use def, I would call the function by something like f(9), which I used the a
0
QTWizard Yes I know, but the case on the topic, it wasnt
, which still work, it seems weird
0
filter is defined like the following:
def filter(f, list0):
List1 = []
for item in list0:
if f(item): # on this line, think of item as "a"
List1.append(item)
return List1
this is not the exact implementation of filter, but i used it to show you where filter call the f function.