Re: [Tutor] Counting Items in a List

2012-06-20 Thread Prasad, Ramit
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 a

Re: [Tutor] Counting Items in a List

2012-06-20 Thread mariocatch
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 wrote: > Yes, I me

Re: [Tutor] Counting Items in a List

2012-06-20 Thread mariocatch
Yes, I meant split(). Was a typo :) On Wed, Jun 20, 2012 at 3:56 PM, Prasad, Ramit 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('.').s

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Prasad, Ramit
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/libra

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Steven D'Aprano
Oğuzhan Öğreden wrote: Hi, I have been learning Python and trying little bits of coding for a while. Recently I tried to have a paragraph and create a list of its words, then counting those words. Here is the code: [code snipped] I can't understand your code! Indentation is messed up badly. I

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Prasad, Ramit
>    para = paragraph.strip('.').strip(',').strip().split() I think you want replace not strip. See http://docs.python.org/library/stdtypes.html#string-methods Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216

Re: [Tutor] Counting Items in a List

2012-06-20 Thread mariocatch
Here's one way, can surely be optimized: *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() wordDict = {} for word in para: word = word.strip('.').st

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Alan Gauld
On 20/06/12 16:20, Martin A. Brown wrote: : should have written something like "counting how many times each : word occured" insted of "counting words". You might like to try out collections.defaultdict(). Even a standard dict would simplify the code dramatically. The defaultdict does sui

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Martin A. Brown
Hello, : Sorry, it seems like I was not clear enough in that statement. I : should have written something like "counting how many times each : word occured" insted of "counting words". You might like to try out collections.defaultdict(). import collections summary = collections.defaul

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Brad Hudson
I think this is more of what you ar looking for: >>> a = 'this is a list with is repeated twice.' >>> a 'this is a list with is repeated twice.' >>> alist = a.strip().split() >>> alist ['this', 'is', 'a', 'list', 'with', 'is', 'repeated', 'twice.'] >>> var='is' >>> alist.count(var) 2 >>> On Wed,

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Oğuzhan Öğreden
Sorry, it seems like I was not clear enough in that statement. I should have written something like "counting how many times each word occured" insted of "counting words". 2012/6/20 mariocatch > Hello, > > Not sure if this is what you're trying to solve, although it sounds like > it based on you

Re: [Tutor] Counting Items in a List

2012-06-20 Thread mariocatch
Hello, Not sure if this is what you're trying to solve, although it sounds like it based on your leading statement. I think what you have is more complicated than it needs to be for something that simple: Why not something like this? *paragraph = """I have been learning Python and trying litt

[Tutor] Counting Items in a List

2012-06-20 Thread Oğuzhan Öğreden
Hi, I have been learning Python and trying little bits of coding for a while. Recently I tried to have a paragraph and create a list of its words, then counting those words. Here is the code: import re > > >> a = """Performs the template substitution, returning a new string. >> mapping is any di