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