[Tutor] Counting Items in a List
mariocatch at gmail.com
Wed Jun 20 22:12:20 CEST 2012
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
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.
>> > 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
>> >>> 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
>> 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 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:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor