473,385 Members | 1,343 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,385 software developers and data experts.

going form a list to a bunch of strings

lets say i have a list called
Expand|Select|Wrap|Line Numbers
  1. thelist = ['student A got a 78 on their exam;\n student B got a 68 on their exam;\n student C got a 58 on their exam;\n']
Now if I wanted to make each word its own string I could do .split(), but lets say I wanted to break it up per sentence (ie:'student A got a 78 on their exam' is one string)

What if I wanted to do it simply by the # of items in the string...that is, which might work in this situation....go ever 8 items (that is, words) then make a new sentence. Of if I wanted to be weird i could go ever 2 words o i'd get...
'student A', 'got a', ' 78 on', 'their exam'

lastly, if i wanted to break up upon a character, such as the semicolon at the end of each sentence.

Thanks
Aug 21 '07 #1
6 1177
bartonc
6,596 Expert 4TB
For the first part, use the optional argument to str.split():
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'student A got a 78 on their exam;\n student B got a 68 on their exam;\n student C got a 58 on their exam;\n'
  2. >>> s
  3. 'student A got a 78 on their exam;\n student B got a 68 on their exam;\n student C got a 58 on their exam;\n'
  4. >>> sentences = s.split('\n')
  5. >>> sentences
  6. ['student A got a 78 on their exam;', ' student B got a 68 on their exam;', ' student C got a 58 on their exam;', '']
Aug 21 '07 #2
ilikepython
844 Expert 512MB
lets say i have a list called
Expand|Select|Wrap|Line Numbers
  1. thelist = ['student A got a 78 on their exam;\n student B got a 68 on their exam;\n student C got a 58 on their exam;\n']
Now if I wanted to make each word its own string I could do .split(), but lets say I wanted to break it up per sentence (ie:'student A got a 78 on their exam' is one string)

What if I wanted to do it simply by the # of items in the string...that is, which might work in this situation....go ever 8 items (that is, words) then make a new sentence. Of if I wanted to be weird i could go ever 2 words o i'd get...
'student A', 'got a', ' 78 on', 'their exam'

lastly, if i wanted to break up upon a character, such as the semicolon at the end of each sentence.

Thanks
Try this for splitting on words:
Expand|Select|Wrap|Line Numbers
  1. def splitwords(words, n): # number of words
  2.     return [' '.join(words[i:i+n]) for i in range(0, len(words) - n, n)]
  3.  
  4. thelist = ['student A got a 78 on their exam;\n student B got a 68 on their exam;\n student C got a 58 on their exam;\n']
  5. words = thelist[0].split()
  6. new = splitwords(words, 2)
  7.  
Aug 21 '07 #3
bartonc
6,596 Expert 4TB
For the first part, use the optional argument to str.split():
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'student A got a 78 on their exam;\n student B got a 68 on their exam;\n student C got a 58 on their exam;\n'
  2. >>> s
  3. 'student A got a 78 on their exam;\n student B got a 68 on their exam;\n student C got a 58 on their exam;\n'
  4. >>> sentences = s.split('\n')
  5. >>> sentences
  6. ['student A got a 78 on their exam;', ' student B got a 68 on their exam;', ' student C got a 58 on their exam;', '']
Don't know why you'd want to do this, but:
Expand|Select|Wrap|Line Numbers
  1. >>> for sentence in sentences:
  2. ...     print [" ".join(tup) for tup in zip(sentence.split()[::2], sentence.split()[1::2])]
  3. ...     
  4. ['student A', 'got a', '78 on', 'their exam;']
  5. ['student B', 'got a', '68 on', 'their exam;']
  6. ['student C', 'got a', '58 on', 'their exam;']
  7. []
  8. >>> 
Aug 21 '07 #4
bartonc
6,596 Expert 4TB
Try this for splitting on words:
Expand|Select|Wrap|Line Numbers
  1. def splitwords(words, n): # number of words
  2.     return [' '.join(words[i:i+n]) for i in range(0, len(words) - n, n)]
  3.  
  4. thelist = ['student A got a 78 on their exam;\n student B got a 68 on their exam;\n student C got a 58 on their exam;\n']
  5. words = thelist[0].split()
  6. new = splitwords(words, 2)
  7.  
I'd like you to notice the "new" in red. "new" is a python reserved word and should not be used as a variable name (just like "list", "dict", etc).
Aug 21 '07 #5
ilikepython
844 Expert 512MB
I'd like you to notice the "new" in red. "new" is a python reserved word and should not be used as a variable name (just like "list", "dict", etc).
Are you sure though?:
Expand|Select|Wrap|Line Numbers
  1. >>> new
  2.  
  3. Traceback (most recent call last):
  4.   File "<pyshell#0>", line 1, in <module>
  5.     new
  6. NameError: name 'new' is not defined
  7. >>> 
  8.  
That's wierd. Maybe it's in older versions?
Aug 21 '07 #6
ilikepython
844 Expert 512MB
Are you sure though?:
Expand|Select|Wrap|Line Numbers
  1. >>> new
  2.  
  3. Traceback (most recent call last):
  4.   File "<pyshell#0>", line 1, in <module>
  5.     new
  6. NameError: name 'new' is not defined
  7. >>> 
  8.  
That's wierd. Maybe it's in older versions?
Oh Opps. It's a module. Not as bad as a reserved word though. Plus, I've never heard of the new module. And just checked, the module is deprecated.
Aug 21 '07 #7

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

Similar topics

3
by: ken.boss | last post by:
I am a python newbie, and am grappling with a fundamental concept. I want to modify a bunch of variables in place. Consider the following: >>> a = 'one' >>> b = 'two' >>> c = 'three' >>>...
15
by: Steve | last post by:
Hi, I've been charged with investigating the possibilities of internationalizing our C++ libraries. std::strings are used all over the place, and unfortunately a mixture of...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
2
by: Bob Johnson | last post by:
Using C#/2.0 I'm writing a small "data translator" utility app that reads data out of a MS Access database and inserts it into a SQL Server database. The source db lists a bunch of names of people...
4
by: Toze | last post by:
I'm using a assembly to load my apllication (ex: Mobi.exe), and now I need to list all forms in my apllication and list all controls (ex: txtname;btnname) inside of each form.
2
by: Billy | last post by:
Hi, It has to be some stupid high school home task , that can be solved in rather straightforward manner. The pain in the @ss is the requirement not to touch the original list, that is: no swap...
25
by: beginner | last post by:
Hi, I am wondering how do I 'flatten' a list or a tuple? For example, I'd like to transform or ] to . Another question is how do I pass a tuple or list of all the aurgements of a function to...
6
by: Dave | last post by:
On my form I have combo boxes. These combo boxes, after updating them, populate respective listboxes that are located below the combo boxes on the same form. I am trying to use a "generate...
3
by: Salad | last post by:
Using A97, SP2, most current jet35. I have a search form. The op enters an id to search/find. If found, a data entry form is presented for that id. This form has 7 or 8 combos, a bunch of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.