473,706 Members | 6,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

going form a list to a bunch of strings

54 New Member
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 1186
bartonc
6,596 Recognized Expert Expert
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 Recognized Expert Contributor
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 Recognized Expert Expert
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 Recognized Expert Expert
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 Recognized Expert Contributor
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 Recognized Expert Contributor
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
2111
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' >>> list = >>> for i in range(len(list)): .... list = list.upper()
15
2662
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 isalpha,isdigit,etc. functions from the C library and C++ locale stuff. To fully embrace i18n I'm wondering if we have to fully make the switch to everything being wide ( wstring, wcin, wcout, wide streams, etc.)
5
6055
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 hits the right and left keys to move this insertion point (cursor) Here is the problem:
2
1735
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 (the particular table structure is not relevant to this post). We need to identify the gender of the people as best as we can. We currently have two text files (MaleNames.txt and FemaleNames.txt) that list typically male or female names,...
4
13357
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
1580
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 between the elements is permitted. O(n^2) is a bunch of time: the list can be even bubble sorted, but I really fail to figure out how the hell I'm supposed to handle the sorting without building any additional structures and the requirement not...
25
4090
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 the function. For example, I have all the arguments of a function in a tuple a=(1,2,3). Then I want to pass each item in the tuple to a function f so that I make a function call f(1,2,3). In perl it is a given, but in python, I haven't figured out
6
9423
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 report" button located on my form to print all of the list box values (that have been updated via selection from combo boxes) from the form to the report. I've tried using a macro with the code: Macro Name: cmdGenerateReport : On Click Action:...
3
1615
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 textboxes, and some command buttons. At the client site yesterday the user got a "too many databases open" after entering a bunch of IDs into the search form. Later that day, after entering a bunch more IDs, she got an out of memory error and the...
0
8695
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9285
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8989
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6614
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5938
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4708
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2499
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2092
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.