I want to split my string in separate sentences. My code is below
import tensorflow as tf
import pandas as pd
text = 'My cat is a great cat, this is very nice, you are doing great job'
text = text.split(',')
Output:
['My cat is a great cat', ' this is very nice', ' you are doing great job']
But I want this output to be
['My cat is a great cat'] ['this is very nice'] ['you are doing great job']
is it possible??
CodePudding user response:
Yes it's possible, we have answers already given above but to match exact expected output as mentioned above use below:
inputlist = "apple,banana,cherry"
print(*[[s] for s in inputlist.split(',')],end=" ")
output:
['apple'] ['banana'] ['cherry']
CodePudding user response:
You can do
text = list(map(lambda x: [x], text.split(',')))
print(text)
[['My cat is a great cat'], [' this is very nice'], [' you are doing great job']]
CodePudding user response:
You need to insert each item in a list. You can use str.strip()
for removing spaces.
>>> text = 'My cat is a great cat, this is very nice, you are doing great job'
>>> [[i.strip()] for i in text.split(',')]
[['My cat is a great cat'], ['this is very nice'], ['you are doing great job']]
CodePudding user response:
You just turn into a list of list with this
text = 'My cat is a great cat, this is very nice, you are doing great job'
text = [[x] for x in text.split(", ")]
CodePudding user response:
Try this, this will put every element of the list into a list
text = 'My cat is a great cat, this is very nice, you are doing great job'
text = [ [i] for i in text.split(',')]
CodePudding user response:
Didn't know if you wanted this in tensorflow
. But if so, here's how you do it,
import tensorflow as tf
text = tf.constant('My cat is a great cat, this is very nice, you are doing great job')
tf.strings.split(text, sep=',')[:, tf.newaxis]
Also, I wonder if your question was about splitting and getting rid of space after the comma. If that's what you needed,
tf.strings.split(text, sep=', ')