counting how often the same word appears in a txt file...But my codeonly prints the last line entry

Discussion in 'Python' started by dgcosgrave@gmail.com, Dec 19, 2012.

  1. Guest

    Hi Iam just starting out with python...My code below changes the txt file into a list and add them to an empty dictionary and print how often the word occurs, but it only seems to recognise and print the last entry of the txt file. Any help would be great.

    tm =open('ask.txt', 'r')
    dict = {}
    for line in tm:
    line = line.strip()
    line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
    line = line.lower()
    list = line.split(' ')
    for word in list:
    if word in dict:
    count = dict[word]
    count += 1
    dict[word] = count
    else:
    dict[word] = 1
    for word, count in dict.iteritems():
    print word + ":" + str(count)
     
    , Dec 19, 2012
    #1
    1. Advertising

  2. Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file

    writes:

    > Hi Iam just starting out with python...My code below changes the txt
    > file into a list and add them to an empty dictionary and print how
    > often the word occurs, but it only seems to recognise and print the
    > last entry of the txt file. Any help would be great.
    >
    > tm =open('ask.txt', 'r')
    > dict = {}
    > for line in tm:
    > line = line.strip()
    > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
    > line = line.lower()
    > list = line.split(' ')
    > for word in list:
    > if word in dict:
    > count = dict[word]
    > count += 1
    > dict[word] = count
    > else:
    > dict[word] = 1
    > for word, count in dict.iteritems():
    > print word + ":" + str(count)


    The "else" clause is mis-indented (rather, mis-unindented).

    Python's "for" statement does have an optional "else" clause. That's
    why you don't get a syntax error. The "else" clause is used after the
    loop finishes normally. That's why it catches the last word.
     
    Jussi Piitulainen, Dec 19, 2012
    #2
    1. Advertising

  3. Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file

    On Wed, 19 Dec 2012 02:45:13 -0800, dgcosgrave wrote:

    > Hi Iam just starting out with python...My code below changes the txt
    > file into a list and add them to an empty dictionary and print how often
    > the word occurs, but it only seems to recognise and print the last entry
    > of the txt file. Any help would be great.
    >
    > tm =open('ask.txt', 'r')
    > dict = {}
    > for line in tm:
    > line = line.strip()
    > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
    > line = line.lower()
    > list = line.split(' ')


    Note: you should use descriptive names. Since this is a list of WORDS, a
    much better name would be "words" rather than list. Also, list is a built-
    in function, and you may run into trouble when you accidentally re-use
    that as a name. Same with using "dict" as you do.

    Apart from that, so far so good. For each line, you generate a list of
    words. But that's when it goes wrong, because you don't do anything with
    the list of words! The next block of code is *outside* the for-loop, so
    it only runs once the for-loop is done. So it only sees the last list of
    words.

    > for word in list:


    The problem here is that you lost the indentation. You need to indent the
    "for word in list" (better: "for word in words") so that it starts level
    with the line above it.

    > if word in dict:
    > count = dict[word]
    > count += 1
    > dict[word] = count


    This bit is fine.

    > else:
    > dict[word] = 1


    But this fails for the same reason! You have lost the indentation.

    A little-known fact: Python for-loops take an "else" block too! It's a
    badly named statement, but sometimes useful. You can write:


    for value in values:
    do_something_with(value)
    if condition:
    break # skip to the end of the for...else
    else:
    print "We never reached the break statement"

    So by pure accident, you lined up the "else" statement with the for loop,
    instead of what you needed:

    for line in tm:
    ... blah blah blah
    for word in words:
    if word in word_counts: # better name than "dict"
    ... blah blah blah
    else:
    ...


    > for word, count in dict.iteritems():
    > print word + ":" + str(count)


    And this bit is okay too.


    Good luck!


    --
    Steven
     
    Steven D'Aprano, Dec 19, 2012
    #3
  4. Thomas Bach Guest

    Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file

    Hi,

    just as a side-note

    On Wed, Dec 19, 2012 at 02:45:13AM -0800, wrote:
    > for word in list:
    > if word in dict:
    > count = dict[word]
    > count += 1
    > dict[word] = count
    > else:
    > dict[word] = 1


    When you got the indentation and names right, you can restate this as

    import collections
    counter = collections.Counter(words)

    in Python 2.7 or as

    import collections
    counter = collections.defaultdict(int)
    for word in words:
    counter[word] += 1

    in Python 2.6

    Regards,
    Thomas.
     
    Thomas Bach, Dec 19, 2012
    #4
  5. Guest

    Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file

    On Wednesday, December 19, 2012 11:55:28 PM UTC+13, Jussi Piitulainen wrote:
    >
    >
    >
    >
    > > Hi Iam just starting out with python...My code below changes the txt

    >
    > > file into a list and add them to an empty dictionary and print how

    >
    > > often the word occurs, but it only seems to recognise and print the

    >
    > > last entry of the txt file. Any help would be great.

    >
    > >

    >
    > > tm =open('ask.txt', 'r')

    >
    > > dict = {}

    >
    > > for line in tm:

    >
    > > line = line.strip()

    >
    > > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')

    >
    > > line = line.lower()

    >
    > > list = line.split(' ')

    >
    > > for word in list:

    >
    > > if word in dict:

    >
    > > count = dict[word]

    >
    > > count += 1

    >
    > > dict[word] = count

    >
    > > else:

    >
    > > dict[word] = 1

    >
    > > for word, count in dict.iteritems():

    >
    > > print word + ":" + str(count)

    >
    >
    >
    > The "else" clause is mis-indented (rather, mis-unindented).
    >
    >
    >
    > Python's "for" statement does have an optional "else" clause. That's
    >
    > why you don't get a syntax error. The "else" clause is used after the
    >
    > loop finishes normally. That's why it catches the last word.


    Thanks for quick reply Jussi...indentation fixed the problem :)
     
    , Dec 19, 2012
    #5
  6. Guest

    Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file

    On Thursday, December 20, 2012 12:03:21 AM UTC+13, Steven D'Aprano wrote:
    > On Wed, 19 Dec 2012 02:45:13 -0800, dgcosgrave wrote:
    >
    >
    >
    > > Hi Iam just starting out with python...My code below changes the txt

    >
    > > file into a list and add them to an empty dictionary and print how often

    >
    > > the word occurs, but it only seems to recognise and print the last entry

    >
    > > of the txt file. Any help would be great.

    >
    > >

    >
    > > tm =open('ask.txt', 'r')

    >
    > > dict = {}

    >
    > > for line in tm:

    >
    > > line = line.strip()

    >
    > > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')

    >
    > > line = line.lower()

    >
    > > list = line.split(' ')

    >
    >
    >
    > Note: you should use descriptive names. Since this is a list of WORDS, a
    >
    > much better name would be "words" rather than list. Also, list is a built-
    >
    > in function, and you may run into trouble when you accidentally re-use
    >
    > that as a name. Same with using "dict" as you do.
    >
    >
    >
    > Apart from that, so far so good. For each line, you generate a list of
    >
    > words. But that's when it goes wrong, because you don't do anything with
    >
    > the list of words! The next block of code is *outside* the for-loop, so
    >
    > it only runs once the for-loop is done. So it only sees the last list of
    >
    > words.
    >
    >
    >
    > > for word in list:

    >
    >
    >
    > The problem here is that you lost the indentation. You need to indent the
    >
    > "for word in list" (better: "for word in words") so that it starts level
    >
    > with the line above it.
    >
    >
    >
    > > if word in dict:

    >
    > > count = dict[word]

    >
    > > count += 1

    >
    > > dict[word] = count

    >
    >
    >
    > This bit is fine.
    >
    >
    >
    > > else:

    >
    > > dict[word] = 1

    >
    >
    >
    > But this fails for the same reason! You have lost the indentation.
    >
    >
    >
    > A little-known fact: Python for-loops take an "else" block too! It's a
    >
    > badly named statement, but sometimes useful. You can write:
    >
    >
    >
    >
    >
    > for value in values:
    >
    > do_something_with(value)
    >
    > if condition:
    >
    > break # skip to the end of the for...else
    >
    > else:
    >
    > print "We never reached the break statement"
    >
    >
    >
    > So by pure accident, you lined up the "else" statement with the for loop,
    >
    > instead of what you needed:
    >
    >
    >
    > for line in tm:
    >
    > ... blah blah blah
    >
    > for word in words:
    >
    > if word in word_counts: # better name than "dict"
    >
    > ... blah blah blah
    >
    > else:
    >
    > ...
    >
    >
    >
    >
    >
    > > for word, count in dict.iteritems():

    >
    > > print word + ":" + str(count)

    >
    >
    >
    > And this bit is okay too.
    >
    >
    >
    >
    >
    > Good luck!
    >
    >
    >
    >
    >
    > --
    >
    > Steven


    Thanks Steven appreciate great info for future coding. i have change names to be more decriptive and corrected the indentation... all works! cheers
     
    , Dec 19, 2012
    #6
  7. Guest

    Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file

    On Thursday, December 20, 2012 12:21:57 AM UTC+13, Thomas Bach wrote:
    > Hi,
    >
    >
    >
    > just as a side-note
    >
    >
    >
    > On Wed, Dec 19, 2012 at 02:45:13AM -0800, :
    >
    > > for word in list:

    >
    > > if word in dict:

    >
    > > count = dict[word]

    >
    > > count += 1

    >
    > > dict[word] = count

    >
    > > else:

    >
    > > dict[word] = 1

    >
    >
    >
    > When you got the indentation and names right, you can restate this as
    >
    >
    >
    > import collections
    >
    > counter = collections.Counter(words)
    >
    >
    >
    > in Python 2.7 or as
    >
    >
    >
    > import collections
    >
    > counter = collections.defaultdict(int)
    >
    > for word in words:
    >
    > counter[word] += 1
    >
    >
    >
    > in Python 2.6
    >
    >
    >
    > Regards,
    >
    > Thomas.


    Thanks Thomas for your time... using 2.7 great!
     
    , Dec 19, 2012
    #7
  8. Guest

    Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file

    On Thursday, December 20, 2012 12:21:57 AM UTC+13, Thomas Bach wrote:
    > Hi,
    >
    >
    >
    > just as a side-note
    >
    >
    >
    > On Wed, Dec 19, 2012 at 02:45:13AM -0800, :
    >
    > > for word in list:

    >
    > > if word in dict:

    >
    > > count = dict[word]

    >
    > > count += 1

    >
    > > dict[word] = count

    >
    > > else:

    >
    > > dict[word] = 1

    >
    >
    >
    > When you got the indentation and names right, you can restate this as
    >
    >
    >
    > import collections
    >
    > counter = collections.Counter(words)
    >
    >
    >
    > in Python 2.7 or as
    >
    >
    >
    > import collections
    >
    > counter = collections.defaultdict(int)
    >
    > for word in words:
    >
    > counter[word] += 1
    >
    >
    >
    > in Python 2.6
    >
    >
    >
    > Regards,
    >
    > Thomas.


    Thanks Thomas for your time... using 2.7 great!
     
    , Dec 19, 2012
    #8
  9. Re: counting how often the same word appears in a txt file...But mycode only prints the last line entry in the txt file

    On Wed, 19 Dec 2012 02:45:13 -0800 (PST), declaimed
    the following in gmane.comp.python.general:


    > tm =open('ask.txt', 'r')
    > dict = {}
    > for line in tm:
    > line = line.strip()
    > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
    > line = line.lower()
    > list = line.split(' ')


    You could (though it gets a bit long) combine some of the above...

    list = line.strip().translate(
    None,
    "the punctuation set"
    ).lower().split()

    # taking advantage that open ()/[]/{} automatically continue on next
    lines

    > for word in list:


    INDENTATION! As coded, you first do the strip/translate/lower/split
    on EACH line of the file... THEN you are processing the words in the
    LAST line processed in the previous loop.

    > if word in dict:
    > count = dict[word]
    > count += 1
    > dict[word] = count
    > else:
    > dict[word] = 1


    More indentation -- I suspect your want the else: and following line
    to be indented the same as the if line...

    Though the whole block can be simplified to

    dict[word] = dict.get(word, 0) + 1

    > for word, count in dict.iteritems():
    > print word + ":" + str(count)

    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Dec 19, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miguel Dias Moura
    Replies:
    4
    Views:
    483
    Hans Kesting
    May 6, 2004
  2. Frost
    Replies:
    8
    Views:
    544
    Vladimir S. Oka
    Feb 10, 2006
  3. David Segall
    Replies:
    9
    Views:
    549
    Joy Beeson
    May 1, 2010
  4. Mmcolli00 Mom
    Replies:
    18
    Views:
    631
    Brian Candler
    Dec 22, 2008
  5. Mirik

    Last word in line, BUT...!!!

    Mirik, Sep 23, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    90
    Mirik
    Sep 23, 2005
Loading...

Share This Page