943,962 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2286
  • Python RSS
Nov 2nd, 2009
0

Removing a substring from string

Expand Post »
I am trying to remove a specific substring from a string... Here are the doctests. I'm just absolutely stumped. Can someone point me in the right direction on where to start?

def remove(sub, s):

"""
>>> remove('an', 'banana')
'bana'
>>> remove('cyc', 'bicycle')
'bile'
>>> remove('iss', 'Mississippi')
'Mippi'
"""


def remove_all(sub, s):
"""
>>> remove('an', 'banana')
'ba'
>>> remove('cyc', 'bicycle')
'bile'
>>> remove('iss', 'Mississippi')
'Mippi'
"""


if __name__ == '__main__':
import doctest
doctest.testmod()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wrobl1rt is offline Offline
3 posts
since Nov 2009
Nov 2nd, 2009
0
Re: Removing a substring from string
A possible implementation of remove_all
python Syntax (Toggle Plain Text)
  1. import re
  2.  
  3. def remove_all(sub, s):
  4. """
  5. >>> remove_all('an', 'banana')
  6. 'ba'
  7. >>> remove_all('cyc', 'bicycle')
  8. 'bile'
  9. >>> remove_all('iss', 'Mississippi')
  10. 'Mippi'
  11. """
  12.  
  13. return re.sub(re.escape(sub), '', s)
  14.  
  15.  
  16. if __name__ == '__main__':
  17. import doctest
  18. doctest.testmod()
Note that a regex is compiled on each call.
Last edited by Gribouillis; Nov 2nd, 2009 at 5:11 pm.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Nov 2nd, 2009
0
Re: Removing a substring from string
Thank you!! would you mind explaining why the implementation of return re.sub(re.escape(sub), '', s) works for the remove_all function specifically?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wrobl1rt is offline Offline
3 posts
since Nov 2009
Nov 2nd, 2009
0
Re: Removing a substring from string
In fact, there is a simpler method
python Syntax (Toggle Plain Text)
  1. def remove_all(sub, s):
  2. """
  3. >>> remove_all('an', 'banana')
  4. 'ba'
  5. >>> remove_all('cyc', 'bicycle')
  6. 'bile'
  7. >>> remove_all('iss', 'Mississippi')
  8. 'Mippi'
  9. """
  10. return s.replace(sub, '')
  11.  
  12. if __name__ == '__main__':
  13. import doctest
  14. doctest.testmod()
I don't understand your question, re.sub replaces every occurrence of a pattern by a given string, that's how it works.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Nov 2nd, 2009
0
Re: Removing a substring from string
oh ok... the book I am going by gave me these problems when I haven't learned about re.sub yet. Ive looked back and it was not even mentioned.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wrobl1rt is offline Offline
3 posts
since Nov 2009
Nov 3rd, 2009
0
Re: Removing a substring from string
you could also try this:

Python Syntax (Toggle Plain Text)
  1. def remove( s, rem ):
  2. newS = ""
  3. i = 0
  4. while i < len( s ):
  5. if s[ i : i + len( rem ) ] == rem:
  6. i += len( rem )
  7. else:
  8. newS += s[ i ]
  9. i += 1
  10. print newS
  11.  
  12. s = "Mississippi"; r = "iss"
  13. remove( s, r )

the old facion way
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 3rd, 2009
1
Re: Removing a substring from string
If
remove('an', 'banana') --> 'bana'
then this should be
remove('iss', 'Mississippi') --> 'Missippi'
So you can use ...
Python Syntax (Toggle Plain Text)
  1. def remove(sub, s):
  2. # replace first sub with empty string
  3. return s.replace(sub, "", 1)
  4.  
  5. # test
  6. print( remove('an', 'banana') ) # --> bana
  7. print( remove('iss', 'Mississippi') ) # --> Missippi
If you want to remove all subs, then use -1 in replace() which actually is the default value ...
Python Syntax (Toggle Plain Text)
  1. def remove_all(sub, s):
  2. # replace all sub with empty string
  3. return s.replace(sub, "", -1)
  4.  
  5. # test
  6. print( remove_all('an', 'banana') ) # --> ba
  7. print( remove_all('iss', 'Mississippi') ) # --> Mippi
Python comes with a wealth of thoroughly tested string functions, might as well use them.
Last edited by vegaseat; Nov 3rd, 2009 at 10:27 am.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Parsing Problem
Next Thread in Python Forum Timeline: File size is increased after pickle





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC