Extract the first word using a lambda function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Extract the first word using a lambda function

Description of the task: Black Box (str -> str) Hello world => Hello a word => a don't touch it => don't greetings, friends => greetings ... and so on ... => and hi => hi ***We need to use just only a lambda function! My attempt: lambda s=' ..., first word': [((i*(i.isalpha()) or i) for i not in ' .,') for i in s.split()]

27th May 2020, 12:58 PM
Andrey Shintar
Andrey Shintar - avatar
6 Answers
+ 2
Thanks, Valmob101, you are cool! I've a little bit modified your function, and so, we've got it!) The right answer is: lambda s: str(''.join([i for i in s.split() if i[0].isalpha()][0])).split(',')[0]
28th May 2020, 11:59 AM
Andrey Shintar
Andrey Shintar - avatar
+ 1
Use this: c = ".,.,.. hello world" a = (lambda x:[i for i in x if i[0].isalpha()][0])(c.split()) print(a)
27th May 2020, 1:33 PM
Valmob101
Valmob101 - avatar
+ 1
Valmob101, I've written the same yesterday ([i for i in s.split() if i.isalpha()==True][0]) , BUT it doesn't pass words like "don't", or like "comma,". We need to except symbols , and '. Take note of the description
28th May 2020, 9:20 AM
Andrey Shintar
Andrey Shintar - avatar
28th May 2020, 9:44 AM
Valmob101
Valmob101 - avatar
0
>>>s= lambda x: x[:x.index(' ')] >>>print(s('hello world')) hello
27th May 2020, 2:57 PM
Ramin.RX7
Ramin.RX7 - avatar
0
Andrex , I fixed my answer(added [0] to i). Let me know whether this is what you want.
28th May 2020, 9:35 AM
Valmob101
Valmob101 - avatar