Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
Hello, could anyone help me with this task
I'm trying to do this test task If you are given a list of names, it should produce a list containing only names that consist of more than 5 characters. You can check the length of a string using the len () function and use the filter () function to define the condition. The thing is that I don't know how to use the len method in a lambda, and how to put the condition for the lambda start what the task wants. If anyone could help me with this I'll appreciate it.
6 Respostas
+ 1
A lambda is and acts like a function. It can accept arguments and return something.
There's an example in the tutorial about how to use lambda as aggregate in a filter() function. May you be inspired from the example
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2461/
+ 1
Ipang thank you, you're right, the problem is that I don't know how to incorporate the function (len) in the lambda in order for to do what the problem asked
+ 1
Have you gotten it figured out now though? I think you only need to make the lambda return a boolean (True/False) value depending on the list element's length.
lambda ele: len( ele ) > 5
This lambda returns True when its argument's length was greater than 5
+ 1
Thank you so much Ipang , your code worked, JesĆŗs Crist bless you, thanks for the help
+ 1
lt = [x for x in str(input('Enter Names: ')).split()]
print(list(filter(lambda i: len(i) > 5, lt)))
+ 1
names = ["David", "John", "Annabelle", "Johnathan", "Veronica"]
result = list(filter(lambda x:len(x)>5,names))
print(result)