[Tutor] Counting Items in a List

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Jun 20 23:08:48 CEST 2012


Please do not top post.

> Sorry for the double-post.
> 
> I don't think I am wrong either. Replace doesn't return a list of words in
> the way that my code sample was setup to do.
> How would you solve it with replace? Maybe an example prove the point
> better.
> 
> -Mario
> On Wed, Jun 20, 2012 at 4:10 PM, mariocatch <mariocatch at gmail.com> wrote:
> Yes, I meant split(). Was a typo :)
> 
> On Wed, Jun 20, 2012 at 3:56 PM, Prasad, Ramit <ramit.prasad at jpmorgan.com>
> wrote:
> Please always post back to the list.
> 
> > Nope, strip() was intended so we get a list back instead of a string.
> Need
> > the list for iteration down below that.
> >> >    para = paragraph.strip('.').strip(',').strip().split()
> >> I think you want replace not strip.
> >>
> >> See http://docs.python.org/library/stdtypes.html#string-methods
> No, you are wrong. If you had looked at the link (or tested the code) you
> would find strip() does not do what you think it does.
> 
> What do you mean by "we get a list back instead of a string"? strip does
> not return a list...it returns a string. split is what returns the list for
> iteration.
> 
> 
> >>> paragraph = "This paragraph contains words once, more than once, and
> possibly not at all either. Figure that one out. "
> >>> para = paragraph.strip('.').strip(',').strip().split()
> >>> print para
> ['This', 'paragraph', 'contains', 'words', 'once,', 'more', 'than',
> 'once,', 'and', 'possibly', 'not', 'at', 'all', 'either.', 'Figure',
> 'that', 'one', 'out.']
> 
> Note the inclusion of 'once,' and 'either.' and 'out.'. Use replace to
> remove
> punctuation instead and then just compare words. Most probably you want to
> lower()
> or upper() the entire paragraph to be thorough, otherwise 'This' and 'this'
>  will
> be counted separately.


You are not reading my replies closely enough. I am talking about *replace*
instead of *strip* and NOT split. Again, if you read the documentation
I linked you would see what I am talking about, but if not just look at the
difference between a and b below.

a = ' Something, is happening here.'.strip(',')
b = ' Something, is happening here.'.replace(',', '')
print a==b
Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list