[Tutor] Counting Items in a List

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Jun 20 21:56:32 CEST 2012


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. 


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