10 Replies - 1081 Views - Last Post: 25 November 2012 - 10:12 PM Rate Topic: -----

#1 q81101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-September 12

Remove " 's " in string

Posted 24 November 2012 - 04:28 PM

suppose there are character 's in the end of word, how can I remove it?
Below, is what I tried.
st = "sss python's is fun's "
for ch in st:
    if ch[-2:] in "'s":   # check if last two index is 's
        st = st.replace(ch, "")



somehow it will remove any s and ' not appear at the end.
How can I just remove the character that have " 's ".

Is This A Good Question/Topic? 0
  • +

Replies To: Remove " 's " in string

#2 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Remove " 's " in string

Posted 24 November 2012 - 04:40 PM

You don't need a for loop. Let me just walk through your code.
st = "sss python's is fun's "
for ch in st:


The for loop here says "for ch in st" - the names are telling enough, ch is a single char.
    if ch[-2:] in "'s":   # check if last two index is 's


ch is a char, so using an index doesn't really make sense.
        st = st.replace(ch, "")


Now we replace any instance of ch with the empty string - right after we (try to) make sure that ch is either "'" or "s".

Okay, so what we want to do is replace every single instance of "'s" with an empty string - that's just what string.replace() does.

st = st.replace("'s", "")

Was This Post Helpful? 2
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7629
  • View blog
  • Posts: 12,858
  • Joined: 19-March 11

Re: Remove " 's " in string

Posted 24 November 2012 - 04:42 PM

EDIT:
Wait a minute, that was wrong, and I see Tayacan got in with the right answer anyway...

This post has been edited by jon.kiparsky: 24 November 2012 - 04:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Remove " 's " in string

Posted 24 November 2012 - 08:28 PM

thats wrong though. he wanted to remove it at end of line.
if st.endswith("'s"): st=st[:-2]

Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7629
  • View blog
  • Posts: 12,858
  • Joined: 19-March 11

Re: Remove " 's " in string

Posted 24 November 2012 - 09:00 PM

That was my wrong answer too, but he says at the end of the word.
If you really want to do it at the end of the word, you might try something clever like
st.replace("'s ", " ")



which would preserve something like "o'sullivan" from replacement. But if you're going to be that clever, you might as well get into regex.
Was This Post Helpful? 2
  • +
  • -

#6 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Remove " 's " in string

Posted 24 November 2012 - 09:54 PM

That'll teach me to post from an android phone. I knew something was going on when you retracted your answer.


Ahh!! Real computer!

Here's cleverer.

" ".join([(lambda x: x.rstrip("'s") if x.endswith("'s") else x)(y) for y in st.strip().split()])



But P4P, what the heck does all that mean?


That means... Join each element of the resultant list formed by the list comprehension with spaces.

The list that the list comprehension will operate on is formed by calling the split method of the original string with default arguments(whitespace characters, like spaces) after calling the strip method of the original string which returns a new string that does not have any whitespace characters at the beginning or end.

For each element in the list, we will call a function (a nameless function, in this case, because I was trying to be clever -- lambda is how we achieve this) that will check each word in the list to see if it ends with "'s", and if it does, strip those characters, else do not change anything in the word. This collection of words will be the resultant list formed by the list comprehension.


Another way of writing exactly(mostly?) that is this:

def strip_apostrophe_s_from_end_of_word(word):
    return word.rstrip("'s") if word.endswith("'s") else word

processed_words = []
for word in st.strip().split():
    processed_words.append(strip_apostrophe_s_from_end_of_word(word))

processed_string = ""
for word in processed_words:
    processed_string += word + " " if processed_words.index(word) != len(processed_words)-1 else word




Instead of defining another function, I created a socalled anonymous one with the lambda statement.

>>> lambda HelloWorldArgument: HelloWorldArgument
<function <lambda> at 0x7f54d3f32758>



That gives us a function. Now, how do we call it?

>>> (lambda HelloWorldArgument: HelloWorldArgument)("Hello world!")
'Hello world!'



What does a regular function look like?
>>> def TheNamePart(HelloWorldArgument): return HelloWorldArgument
>>> TheNamePart
<function TheNamePart at 0x7f87d9dd47d0>
>>> TheNamePart("Hello World!")
'Hello World!'




See! Not so bad, huh?

Let's incorporate it into the verbose example
processed_words = []
for word in st.strip().split():
    processed_words.append((lambda x: x.rstrip("'s") if x.endswith("'s") else x)(word))

processed_string = ""
for word in processed_words:
    processed_string += word + " " if processed_words.index(word) != len(processed_words)-1 else word




Now let's incorporate a list comprehension.

processed_words = [(lambda x: x.rstrip("'s") if x.endswith("'s") else x)(word) for word in st.strip().split()]

processed_string = ""
for word in processed_words:
    processed_string += word + " " if processed_words.index(word) != len(processed_words)-1 else word





Now let's get rid of that final loop with a join statement.

" ".join([(lambda x: x.rstrip("'s") if x.endswith("'s") else x)(word) for word in st.strip().split()])



Boom! Clever! Python is WAY more fun than anyone truly knows. I doubt Guido Van Rossum even knows how fun Python can truly be.

This post has been edited by Python_4_President: 25 November 2012 - 12:38 AM

Was This Post Helpful? 2
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7629
  • View blog
  • Posts: 12,858
  • Joined: 19-March 11

Re: Remove " 's " in string

Posted 25 November 2012 - 05:32 PM

As I say, though, it's easier to use regex:
import re
re.sub(r"'s(\W)", r"\1", st)


that is: replace "'s" when followed by a non-word character with the non-word character that was found, in the string st.
But your way is nice too.
Was This Post Helpful? 2
  • +
  • -

#8 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Remove " 's " in string

Posted 25 November 2012 - 05:42 PM

Minor fix to catch 's at end of line not containing newline character, as in:

>>> st = "sss Python's is fun's"
>>> re.sub(r"'s(\W)", r"\1", st)
"sss Python is fun's"
>>> re.sub(r"'s(\W|$)", r"\1", st)
'sss Python is fun'




EDIT: Woops, forgot my money sign. Now it won't nuke O'sullivan.. Poor O'sullivan, such a weird name, unusual enough to be mishandled.


>>> st = "sss Daniel O'Daniels  O'sullivan thinks Python's is fun's"
>>> re.sub(r"'s(\W|$)", r"\1", st)
"sss Daniel O'Daniels  O'sullivan thinks Python is fun"


This post has been edited by Python_4_President: 25 November 2012 - 05:50 PM

Was This Post Helpful? 2
  • +
  • -

#9 q81101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-September 12

Re: Remove " 's " in string

Posted 25 November 2012 - 09:47 PM

View PostPython_4_President, on 25 November 2012 - 05:42 PM, said:

Minor fix to catch 's at end of line not containing newline character, as in:

>>> st = "sss Python's is fun's"
>>> re.sub(r"'s(\W)", r"\1", st)
"sss Python is fun's"
>>> re.sub(r"'s(\W|$)", r"\1", st)
'sss Python is fun'




EDIT: Woops, forgot my money sign. Now it won't nuke O'sullivan.. Poor O'sullivan, such a weird name, unusual enough to be mishandled.


>>> st = "sss Daniel O'Daniels  O'sullivan thinks Python's is fun's"
>>> re.sub(r"'s(\W|$)", r"\1", st)
"sss Daniel O'Daniels  O'sullivan thinks Python is fun"




thank for help

Now, I wonder how to remove the apostrophe at the beginning

like " 'sss python 'fun"

and Can you explain st = re.sub(r"'s(\W|$)", r"\1", st) function? Don't really understand the symbol stuff and \1 mean
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7629
  • View blog
  • Posts: 12,858
  • Joined: 19-March 11

Re: Remove " 's " in string

Posted 25 November 2012 - 10:04 PM

That's regex - regular expression syntax. If you google around, you'll find some material on it. There's a couple of pages on it at the python pages - look at the re module.

In a nutshell, regular expressions allow you to define a formal pattern which can be composed of literal characters and strings of characters, or of classes of characters.
In this case, we're matching on the literal string "'s", when followed by and non-word character (\w indicates the class of 'word characters' - things which appear in words - and \W indicates the complement of that class, ie, everything that doesn't appear in a word). The non-word character is "captured" by the parentheses, which means it's available to refer to in the replacement side of the expression. \1 is the back-reference to the captured non-word character. the r preceding the strings indicates that they should be read as raw strings - this helps with the backslash escaping.
re.sub is the "substitute" function in the re module. st is your original string. I think that covers most of it, but you're going to have to do your own research if you want to understand any of it. Your first stop is probably the docs for the re module, then read some of the various material floating around on regex to get into the details.
This is why I didn't go into the regex solution in my original response. It's incredibly powerful, but not something you can really make use of without a certain commitment of effort. It's a lot like learning a small language. A lot of work, but well worth the effort.
Was This Post Helpful? 1
  • +
  • -

#11 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Remove " 's " in string

Posted 25 November 2012 - 10:12 PM

That's actually a pretty difficult topic on which several very heavy books have been written.

>>> st = "'sss's python's is o'sullivan fun 'fun"
>>> re.sub("((?<=\W)'|(?<=^)')(\S+)", r"\2", re.sub("'s(\W|$)", r"\1", st))
"sss python is o'sullivan fun fun"




That would do it, but it took me a long time to learn how to say things like that.

I'd spend some time in the python interpreter reading the output of help(str) starting at the point where things don't have names with __ in them.


The most helpful resource for me has been the Python documentation on the re module

A good book that helped me out a bit once I knew most of the things you could do with regular expressions is mastering regular expressions

A good tutorial I read a few times is the one on regular-expressions.info

A good text editor to try is notepad++ with the RegEx Helper plugin.

The plugin lets you test your regular expressions on actual text in the editor, which is totally awesome.

Like I said, though. Even with all these great resources, it still took a long time. It's worth it, though. The regular expressions in Python's re module work (mostly) the same way as in other languages AND utilities. If you plan on doing a lot of data parsing, it definitely helps to be friends with them.


The approach I gave you earlier with the string methods is the most accessible to you.

you can see if a string starts with a certain character, or ends with one. You can split the string into substrings which are easier to test against, and then you can put them back together after you make your changes.


You can modify this code (which you should be able to read and understand) to handle your new task.

def strip_apostrophe_s_from_end_of_word(word):
    return word.rstrip("'s") if word.endswith("'s") else word

processed_words = []
for word in st.strip().split():
    word = strip_apostrophe_s_from_end_of_word(word)
    processed_words.append(word)

processed_string = ""
for word in processed_words:
    processed_string += word + " " if processed_words.index(word) != len(processed_words)-1 else word


This post has been edited by Python_4_President: 25 November 2012 - 10:33 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1