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

the str.split(sep,maxsplit) function

P: 4
given:
my = 'my name is david'

i would like to make this into a list with 2 values
i would like the list to be:
mylist = ['my name is','david']

so i try using:
mylist = my.split(' ',1)

but i soon find it splits left to right which returns:
mylist = ['my','name is david']

to avoid having to use alot of other code, i am wondering if there is a way to make it split right to left or reverse of the way its splitting?
Oct 29 '11 #1

✓ answered by bvdet

Following are a couple of ways, but each requires additional code.
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'my name is david'
  2. >>> s.split()
  3. ['my', 'name', 'is', 'david']
  4. >>> s1 = 'my david'
  5. >>> s2 = 'david'
  6. >>> def split_once_rightmost(s):
  7. ...     seq = s.split()
  8. ...     if len(seq) > 1:
  9. ...         return [" ".join(seq[0:-1]), seq[-1]]
  10. ...     return seq
  11. ... 
  12. >>> split_once_rightmost(s)
  13. ['my name is', 'david']
  14. >>> split_once_rightmost(s1)
  15. ['my', 'david']
  16. >>> split_once_rightmost(s2)
  17. ['david']
  18. >>> seq = s.replace(" ", "***", s.count(" ")-1).split(" ")
  19. >>> seq[0] = seq[0].replace("***", " ")
  20. >>> seq
  21. ['my name is', 'david']
  22. >>>

Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Following are a couple of ways, but each requires additional code.
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'my name is david'
  2. >>> s.split()
  3. ['my', 'name', 'is', 'david']
  4. >>> s1 = 'my david'
  5. >>> s2 = 'david'
  6. >>> def split_once_rightmost(s):
  7. ...     seq = s.split()
  8. ...     if len(seq) > 1:
  9. ...         return [" ".join(seq[0:-1]), seq[-1]]
  10. ...     return seq
  11. ... 
  12. >>> split_once_rightmost(s)
  13. ['my name is', 'david']
  14. >>> split_once_rightmost(s1)
  15. ['my', 'david']
  16. >>> split_once_rightmost(s2)
  17. ['david']
  18. >>> seq = s.replace(" ", "***", s.count(" ")-1).split(" ")
  19. >>> seq[0] = seq[0].replace("***", " ")
  20. >>> seq
  21. ['my name is', 'david']
  22. >>>
Oct 29 '11 #2

P: 1
Expand|Select|Wrap|Line Numbers
  1. >>> my = 'my name is david'
  2. >>> import re
  3. >>> re.findall('(.*)\s(.*)', my)
  4. [('my name is', 'david')]
  5.  
or
Expand|Select|Wrap|Line Numbers
  1. >>> my
  2. 'my name is david'
  3. >>> lst = my.split(' ')
  4. >>> lst
  5. ['my', 'name', 'is', 'david']
  6. >>> result = []
  7. >>> result.append(' '.join(lst[:-1]))
  8. >>> result.append(lst[-1])
  9. >>> result
  10. ['my name is', 'david']
  11.  
Oct 30 '11 #3

bvdet
Expert Mod 2.5K+
P: 2,851
I knew there would be a re solution. Thanks for posting basilho. You can use match object method group to create the OP's mylist.
Expand|Select|Wrap|Line Numbers
  1. >>> my = 'my name is david'
  2. >>> import re
  3. >>> patt = re.compile('(.*)\s(.*)')
  4. >>> m = patt.match(my)
  5. >>> [m.group(1), m.group(2)]
  6. ['my name is', 'david']
  7. >>> 
Oct 30 '11 #4

P: 7
The very basics of lists:
Expand|Select|Wrap|Line Numbers
  1. >>> my = my.split()
  2. >>> print (' '.join(my[:-1]), my[-1])
  3. ('my name is', 'david')
  4.  
Nov 14 '11 #5

Post your reply

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