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'] **
9/24/2020 2:58:41 PMSob Xav
17 AnswersNew Answer
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)
Sob Xav **They can be created using functions and the 'yield' statement. [ Edited: ] def words(): for i in txt.split(): yield i
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
Igor Romasenko You are very welcome buddy!👍🍻🍻 I hope it was helpful.😉
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!
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!
Danijel Ivanović, thanks a lot!
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)))
Hi , remove that list function from the print statement , x.split(" ") is already returning a list with words seperated by space
Danijel, the problem is that the input is a string
Thank you all for the help 🙏🙏🙏
def slt(pera): for word in pera.split(): yield word x=input() print(list(slt(x)))
txt = input() def words(x): #your code goes here return x.split(" ") print(words(txt)) The shortest version: print(input().split(" "))
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
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.
txt = input() def words(txt): #your code goes here lista = txt.split(' ') for w in lista: yield w print(list(words(txt)))
txt = input() def words(): b = txt.split() return b print(list(words()))