473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Given a string how do you create a list containing sublists?

you have to write a function which takes in the parameter text (which is a string). And returns a list containing sublist. each sublist contains the sentence in the text. sentences are separated using "." , "?", or "!"

example
if text is "hello, how are you? i am fine thank you. what a lovely day!"
the result should be [["hello", "how", "are", "you"],
["i", "am", "fine", "thank", "you"],["what", "a", "lovely", "day"]]

all text will be entered in as lowercase
Oct 26 '10 #1
2 3409
Glenton
391 Expert 256MB
This interactive session should help:
Expand|Select|Wrap|Line Numbers
  1. >>> T="hello, how are you? i am fine thank you. what a lovely day!"
  2. >>> T.replace("?",".")
  3. 'hello, how are you. i am fine thank you. what a lovely day!'
  4. >>> T=T.replace("?",".")
  5. >>> T=T.replace("!",".")
  6. >>> T
  7. 'hello, how are you. i am fine thank you. what a lovely day.'
  8. >>> T=T.replace(",","")
  9. >>> T
  10. 'hello how are you. i am fine thank you. what a lovely day.'
  11. >>> L=T.split(".")
  12. >>> L
  13. ['hello how are you', ' i am fine thank you', ' what a lovely day', '']
  14. >>> L2=[]
  15. >>> for l in L:
  16.     L2.append(l.split(" "))
  17.  
  18.  
  19. >>> 
  20. >>> L2
  21. [['hello', 'how', 'are', 'you'], ['', 'i', 'am', 'fine', 'thank', 'you'], ['', 'what', 'a', 'lovely', 'day'], ['']]
  22. >>> 
It's not quite right - you need to remove the final full-stop from the test with something like:
Expand|Select|Wrap|Line Numbers
  1. if T[-1]==".":
  2.     T=T[:-1]
Oct 26 '10 #2
Thanks a lot for your time and help. I tested the code and it works as expected.
Oct 26 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: AMT2K5 | last post by:
Hello, I have an assignment to do in class which requires me to create a single line editor not a multi-line editor. I am confused about what this means, I would appreciate any clarification....
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
10
by: kd | last post by:
Hi All, Is there a command to check whether a given string is present in a text file, without having to read the contents of the file, a line at a time and then check for the given string? ...
8
by: varsha.gadekar | last post by:
what is the easy way to check if the given string is substring of other string only using c++?
4
by: manstey | last post by:
Hi, Is there a neat way to write a function that can receive either a string or a list of strings, and then if it receives a string it manipulates that, otherwise it manipulates each string in...
1
by: evansuth777 | last post by:
I am trying to sort a List containing arrays String. I have tried list.sort(). I have been reading up on it and it seems i have to use a comparator. Can anyone help me? Thanks
2
by: Kelly B | last post by:
I tried to write a code which would reverse the order of words in a given string. I.e if Input String=The C Programming Language Output String=Language Programming C The Here is the code...
1
by: redgrL86 | last post by:
Hi, I am trying to figure out how to see if any of a given set of childnodes equals a given string. For example, in the XML and XSL code below, I want the text in the "xsl:when" statement to output...
10
by: mandyj | last post by:
Given String:- ST200STT:35697600114106009,20070407,08:09:00,+18.511246,+73.842756,1.430,332.87,1,0000,0 I wanna separate this string in the format below,pls help me out... 35697600114106009 =...
16
by: saki | last post by:
Write a program to print all the permutations of a given string.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.