[Tutor] Counting Items in a List

mariocatch mariocatch at gmail.com
Wed Jun 20 22:10:02 CEST 2012


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.
>
>
> 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.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120620/e78d5b62/attachment-0001.html>


More information about the Tutor mailing list