By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,825 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,825 IT Pros & Developers. It's quick & easy.

going form a list to a bunch of strings

P: 54
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
Share this Question
Share on Google+
6 Replies

bartonc
Expert 5K+
P: 6,596
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
Expert 100+
P: 844
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
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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
Expert 100+
P: 844
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
Expert 100+
P: 844
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

Post your reply

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