<br><br><div class="gmail_quote">On Wed, Nov 4, 2009 at 4:27 AM, Tim Chase <span dir="ltr"><<a href="mailto:python.list@tim.thechases.com">python.list@tim.thechases.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">kylin wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I need to remove the word if it appears in the paragraph twice. could<br>
some give me some clue or some useful function in the python.<br>
</blockquote>
<br></div>
Sounds like homework.  To fail your class, use this one:<br>
<br>
>>> p = "one two three four five six seven three four eight"<br>
>>> s = set()<br>
>>> print ' '.join(w for w in p.split() if not (w in s or s.add(w)))<br>
one two three four five six seven eight<br>
<br>
which is absolutely horrible because it mutates the set within the list comprehension.  The passable solution would use a for-loop to iterate over each word in the paragraph, emitting it if it hadn't already been seen.  Maintain those words in set, so your words know how not to be seen. ("Mr. Nesbitt, would you please stand up?")<br>

<br></blockquote><div> </div><div>  Can we use inp_paragraph.count(iter_word) to make it simple ?<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

This also assumes your paragraph consists only of words and whitespace.  But since you posted your previous homework-sounding question on stripping out non-word/whitespace characters, you'll want to look into using a regexp like "[\w\s]" to clean up the cruft in the paragraph.  Neither solution above preserves non white-space/word characters, for which I'd recommend using a re.sub() with a callback.  Such a callback class might look something like<br>

<br>
>>> class Dedupe:<br>
...     def __init__(self):<br>
...             self.s = set()<br>
...     def __call__(self, m):<br>
...             w = m.group(0)<br>
...             if w in self.s: return ''<br>
...             self.s.add(w)<br>
...             return w<br>
...<br>
>>> r.sub(Dedupe(), p)<br>
<br>
where I leave the definition of "r" to the student.  Also beware of case-differences for which you might have to normalize.<br>
<br>
You'll also want to use more descriptive variable names than my one-letter tokens.<br>
<br>
-tkc<div><div></div><div class="h5"><br>
<br>
<br>
<br>
<br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Yours,<br>S.Selvam<br>