Question generator text split | Sololearn: Learn to code for FREE!
+ 6

Question generator text split

I struggle with the pro version code exercice. Please help me The exercice with pro version is ** Given a string as input, create a generator function that splits the string into separate words and outputs the resulting list. Sample Input This is some text Sample Output ['This', 'is', 'some', 'text'] **

24th Sep 2020, 2:58 PM
Sob Xav
Sob Xav - avatar
27 ответов
+ 18
Igor Romasenko also, you can print the values using for loop and generator: def words(): str = txt.split() yield str getwords = words() for w in getwords: print(w)
27th Sep 2020, 9:09 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 14
Sob Xav **They can be created using functions and the 'yield' statement. [ Edited: ] def words(): for i in txt.split(): yield i
25th Sep 2020, 3:19 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 11
Igor Romasenko the task says: split the string into separate words and output the resulting list. txt = input() .. print(list(words())) [ Edited: ] To iterate over words of a string: • Split the string. The common delimiter between words in a string is space. The split() returns an array. Each element in the array is a word.     str = txt.split() • Use for loop to iterate over the words present in the array.     for s in str: and than use 'yield' statement: **The 'yield' statement is used to define a generator, replacing the 'return' of a function to provide a result. • https://code.sololearn.com/c7gIWDrcNljm/?ref=app
27th Sep 2020, 7:46 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 8
Igor Romasenko You are very welcome buddy!👍🍻🍻 I hope it was helpful.😉
27th Sep 2020, 3:03 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 6
What language you are talking about? Before you can get help, please do a try by yourself first, store it in playground and post your attempt here. Thanks!
24th Sep 2020, 3:01 PM
Lothar
Lothar - avatar
+ 4
Sob Xav , is the description you gave us really the once that is used in the task description? Can you just copy and past it? Thanks!
24th Sep 2020, 3:50 PM
Lothar
Lothar - avatar
+ 4
txt = input() def words(txt): #your code goes here lista = txt.split(' ') for w in lista: yield w print(list(words(txt)))
28th Jun 2021, 7:39 AM
Mohamed Ikram Ifham
Mohamed Ikram Ifham - avatar
+ 4
txt = input() def gen_list_str(txt): for word in txt.split(''): yield word print(list(gen_list_str(txt))) What's wrong with this code? Someone please help me.
19th Sep 2022, 3:46 PM
Vaishnavi Mani
+ 2
Vaishnavi Mani , the code uses an empty string as separator which creates an error. for default splitting at a space, we can just let the parenthesis of split() function empty. use: for word in txt.split():
20th Sep 2022, 2:55 PM
Lothar
Lothar - avatar
+ 1
Sorry, it is python. Code but it is clearly not making the job and not using a generator as requested. I'm lost... txt = input() def words(x): #your code goes here x.split(" ") return x print(list(words(txt)))
24th Sep 2020, 3:03 PM
Sob Xav
Sob Xav - avatar
+ 1
Hi , remove that list function from the print statement , x.split(" ") is already returning a list with words seperated by space
24th Sep 2020, 3:07 PM
Abhay
Abhay - avatar
+ 1
Danijel, the problem is that the input is a string
27th Sep 2020, 1:03 AM
Igor R
Igor R - avatar
+ 1
Danijel Ivanović, thanks a lot!
27th Sep 2020, 1:50 PM
Igor R
Igor R - avatar
+ 1
Thank you all for the help 🙏🙏🙏
27th Sep 2020, 3:18 PM
Sob Xav
Sob Xav - avatar
+ 1
def slt(pera): for word in pera.split(): yield word x=input() print(list(slt(x)))
3rd Apr 2021, 6:43 AM
Mahanand Yadav
Mahanand Yadav - avatar
+ 1
txt = input() def words(x): #your code goes here return x.split(" ") print(words(txt)) The shortest version: print(input().split(" "))
28th Jun 2021, 1:33 PM
Eduards
+ 1
txt = input() def words(): return txt.split() print(list(words()))
7th Aug 2022, 5:53 PM
Saeed Karrabi
Saeed Karrabi - avatar
+ 1
Lothar Thanks alot
21st Sep 2022, 2:51 AM
Vaishnavi Mani
0
Lothar, the full description is above in my first message, and the empty code is : txt = input() def words(): #your code goes here I guess I will skip this one... Too weird
24th Sep 2020, 3:55 PM
Sob Xav
Sob Xav - avatar
0
txt = input()#1. Gets input text def words(): #your code goes here l=txt.split(' ')#2. Store each word as a list item for i in l: yield i #3. yield each item one at a time to the print statement print(list(words())) #4. Converts each yielded value to a list item and prints.
3rd Jun 2021, 7:10 PM
Shubhang