lets say i have a list called -
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
6 1177
For the first part, use the optional argument to str.split(): -
>>> 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'
-
>>> 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'
-
>>> sentences = s.split('\n')
-
>>> sentences
-
['student A got a 78 on their exam;', ' student B got a 68 on their exam;', ' student C got a 58 on their exam;', '']
lets say i have a list called -
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: -
def splitwords(words, n): # number of words
-
return [' '.join(words[i:i+n]) for i in range(0, len(words) - n, n)]
-
-
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']
-
words = thelist[0].split()
-
new = splitwords(words, 2)
-
For the first part, use the optional argument to str.split(): -
>>> 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'
-
>>> 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'
-
>>> sentences = s.split('\n')
-
>>> sentences
-
['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: -
>>> for sentence in sentences:
-
... print [" ".join(tup) for tup in zip(sentence.split()[::2], sentence.split()[1::2])]
-
...
-
['student A', 'got a', '78 on', 'their exam;']
-
['student B', 'got a', '68 on', 'their exam;']
-
['student C', 'got a', '58 on', 'their exam;']
-
[]
-
>>>
Try this for splitting on words: -
def splitwords(words, n): # number of words
-
return [' '.join(words[i:i+n]) for i in range(0, len(words) - n, n)]
-
-
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']
-
words = thelist[0].split()
-
new = splitwords(words, 2)
-
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).
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?: -
>>> new
-
-
Traceback (most recent call last):
-
File "<pyshell#0>", line 1, in <module>
-
new
-
NameError: name 'new' is not defined
-
>>>
-
That's wierd. Maybe it's in older versions?
Are you sure though?: -
>>> new
-
-
Traceback (most recent call last):
-
File "<pyshell#0>", line 1, in <module>
-
new
-
NameError: name 'new' is not defined
-
>>>
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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'
>>>...
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |