472,110 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 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 974
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

Post your reply

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

Similar topics

3 posts views Thread by ken.boss | last post: by
15 posts views Thread by Steve | last post: by
5 posts views Thread by John N. | last post: by
4 posts views Thread by Toze | last post: by
3 posts views Thread by Salad | last post: by
reply views Thread by leo001 | last post: by

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.