Counting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy

    #1

    Counting

    Hi, the file below will print all the keywords in a file and also the
    line # of the keyword. What I couldn't figure out is to count those
    keywords per line. For example - "Line #1 has 3 keywords"

    Can I do like -

    total[j] = total[j] + numwords(k)
    "Line number %d has %d keywords" % (j, total[j])

    Seems sort of "illegal" in Python?



    -------------------------------------------------
    import keyword, sys, string, fileinput
    def numwords(s):
    list = string.split(s)
    return len(list)

    # Get the file name either from the command-line or the user
    if len(sys.argv) != 2:
    name = raw_input("Ente r the file name: ")
    else:
    name = sys.argv[1]

    inp = open(name,"r")
    linelist = inp.readlines()
    total, words,lines = 0, 0, 0

    for i in range(len(linel ist)):
    line = linelist[i]
    tempwords = line.split()
    for k in tempwords:
    if keyword.iskeywo rd(k):
    total = total + numwords(k)
    j = i + 1
    print" The word * %s * belongs in line number: %d" % (k,
    j)

    print "Total keywords in this file are: %d" %(total)

  • James Stroud

    #2
    Re: Counting

    Andy wrote:
    Hi, the file below will print all the keywords in a file and also the
    line # of the keyword. What I couldn't figure out is to count those
    keywords per line. For example - "Line #1 has 3 keywords"
    >
    Can I do like -
    >
    total[j] = total[j] + numwords(k)
    "Line number %d has %d keywords" % (j, total[j])
    >
    Seems sort of "illegal" in Python?
    >
    >
    >
    -------------------------------------------------
    import keyword, sys, string, fileinput
    def numwords(s):
    list = string.split(s)
    return len(list)
    >
    # Get the file name either from the command-line or the user
    if len(sys.argv) != 2:
    name = raw_input("Ente r the file name: ")
    else:
    name = sys.argv[1]
    >
    inp = open(name,"r")
    linelist = inp.readlines()
    total, words,lines = 0, 0, 0
    >
    for i in range(len(linel ist)):
    line = linelist[i]
    tempwords = line.split()
    for k in tempwords:
    if keyword.iskeywo rd(k):
    total = total + numwords(k)
    j = i + 1
    print" The word * %s * belongs in line number: %d" % (k,
    j)
    >
    print "Total keywords in this file are: %d" %(total)
    >
    You probably want something that goes a little like this:

    for i,line in enumerate(linel ist):
    for k in line.split():
    if keyword.iskeywo rd(k):
    total += line.count(k)
    print "The word '%s' belongs in line num: %d" % (k, i+1)

    print "Total keyords are: %d" % total

    James

    Comment

    • James Stroud

      #3
      Re: Counting

      James Stroud wrote:
      Andy wrote:
      >Hi, the file below will print all the keywords in a file and also the
      >line # of the keyword. What I couldn't figure out is to count those
      >keywords per line. For example - "Line #1 has 3 keywords"
      >>
      >Can I do like -
      >>
      >total[j] = total[j] + numwords(k)
      >"Line number %d has %d keywords" % (j, total[j])
      >>
      >Seems sort of "illegal" in Python?
      >>
      >>
      >>
      >-------------------------------------------------
      >import keyword, sys, string, fileinput
      >def numwords(s):
      > list = string.split(s)
      > return len(list)
      >>
      ># Get the file name either from the command-line or the user
      >if len(sys.argv) != 2:
      > name = raw_input("Ente r the file name: ")
      >else:
      > name = sys.argv[1]
      >>
      >inp = open(name,"r")
      >linelist = inp.readlines()
      >total, words,lines = 0, 0, 0
      >>
      >for i in range(len(linel ist)):
      > line = linelist[i]
      > tempwords = line.split()
      > for k in tempwords:
      > if keyword.iskeywo rd(k):
      > total = total + numwords(k)
      > j = i + 1
      > print" The word * %s * belongs in line number: %d" % (k,
      >j)
      >>
      >print "Total keywords in this file are: %d" %(total)
      >>
      >
      You probably want something that goes a little like this:
      >
      for i,line in enumerate(linel ist):
      for k in line.split():
      if keyword.iskeywo rd(k):
      total += line.count(k)
      print "The word '%s' belongs in line num: %d" % (k, i+1)
      >
      print "Total keyords are: %d" % total
      >
      James
      Oops, that over-counts, I forgot to put a continue in. Also, keeping a
      cache of the split line will probably be faster.

      for i,line in enumerate(linel ist):
      line = line.split()
      for k in line:
      if keyword.iskeywo rd(k):
      total += line.count(k)
      print "The word '%s' belongs in line num: %d" % (k, i+1)
      continue

      print "Total keyords are: %d" % total


      James

      Comment

      • James Stroud

        #4
        Re: Counting

        James Stroud wrote:
        James Stroud wrote:
        >Andy wrote:
        >>Hi, the file below will print all the keywords in a file and also the
        >>line # of the keyword. What I couldn't figure out is to count those
        >>keywords per line. For example - "Line #1 has 3 keywords"
        >>>
        >>Can I do like -
        >>>
        >>total[j] = total[j] + numwords(k)
        >>"Line number %d has %d keywords" % (j, total[j])
        >>>
        >>Seems sort of "illegal" in Python?
        >>>
        >>>
        >>>
        >>-------------------------------------------------
        >>import keyword, sys, string, fileinput
        >>def numwords(s):
        >> list = string.split(s)
        >> return len(list)
        >>>
        >># Get the file name either from the command-line or the user
        >>if len(sys.argv) != 2:
        >> name = raw_input("Ente r the file name: ")
        >>else:
        >> name = sys.argv[1]
        >>>
        >>inp = open(name,"r")
        >>linelist = inp.readlines()
        >>total, words,lines = 0, 0, 0
        >>>
        >>for i in range(len(linel ist)):
        >> line = linelist[i]
        >> tempwords = line.split()
        >> for k in tempwords:
        >> if keyword.iskeywo rd(k):
        >> total = total + numwords(k)
        >> j = i + 1
        >> print" The word * %s * belongs in line number: %d" % (k,
        >>j)
        >>>
        >>print "Total keywords in this file are: %d" %(total)
        >>>
        >>
        >You probably want something that goes a little like this:
        >>
        >for i,line in enumerate(linel ist):
        > for k in line.split():
        > if keyword.iskeywo rd(k):
        > total += line.count(k)
        > print "The word '%s' belongs in line num: %d" % (k, i+1)
        >>
        >print "Total keyords are: %d" % total
        >>
        >James
        >
        Oops, that over-counts, I forgot to put a continue in. Also, keeping a
        cache of the split line will probably be faster.
        >
        for i,line in enumerate(linel ist):
        line = line.split()
        for k in line:
        if keyword.iskeywo rd(k):
        total += line.count(k)
        print "The word '%s' belongs in line num: %d" % (k, i+1)
        continue
        >
        print "Total keyords are: %d" % total
        >
        >
        James
        I should really wait until I've had some coffee. Not continue, but break!


        for i,line in enumerate(linel ist):
        line = line.split()
        for k in line:
        if keyword.iskeywo rd(k):
        total += line.count(k)
        print "The word '%s' belongs in line num: %d" % (k, i+1)
        break

        print "Total keyords are: %d" % total

        Comment

        • rockmode@gmail.com

          #5
          Re: Counting

          That's a short, abridged version of my code :) But, what I want is to
          count total# of keywords per line and print 'em. Rather than
          printing :

          The word 'and' belongs in line num: 1
          The word 'del' belongs in line num: 1
          The word 'from' belongs in line num: 1

          I want to print " Line #1 has 3 keywords"

          ;)

          You probably want something that goes a little like this:
          >
          for i,line in enumerate(linel ist):
          for k in line.split():
          if keyword.iskeywo rd(k):
          total += line.count(k)
          print "The word '%s' belongs in line num: %d" % (k, i+1)
          >
          print "Total keyords are: %d" % total
          >
          James

          Comment

          • castironpi@gmail.com

            #6
            Re: Counting

            On Apr 29, 2:11 pm, Andy <andy.rockf...@ gmail.comwrote:
            Hi, the file below will print all the keywords in a file and also the
            line # of the keyword. What I couldn't figure out is to count those
            keywords per line. For example - "Line #1 has 3 keywords"
            >
            Can I do like -
            >
            total[j] = total[j] + numwords(k)
            "Line number %d has %d keywords" % (j, total[j])
            >
            Seems sort of "illegal" in Python?
            >
            -------------------------------------------------
            import keyword, sys, string, fileinput
            def numwords(s):
            list = string.split(s)
            return len(list)
            >
            # Get the file name either from the command-line or the user
            if len(sys.argv) != 2:
            name = raw_input("Ente r the file name: ")
            else:
            name = sys.argv[1]
            >
            inp = open(name,"r")
            linelist = inp.readlines()
            total, words,lines = 0, 0, 0
            >
            for i in range(len(linel ist)):
            line = linelist[i]
            tempwords = line.split()
            for k in tempwords:
            if keyword.iskeywo rd(k):
            total = total + numwords(k)
            j = i + 1
            print" The word * %s * belongs in line number: %d" % (k,
            j)
            >
            print "Total keywords in this file are: %d" %(total)
            tempwords = line.split()
            for k in tempwords:
            linec = 0
            if keyword.iskeywo rd(k):
            total = total + numwords(k)
            j = i + 1
            linec += 1
            print" The word * %s * belongs in line number: %d" % (k,
            j)
            print "%i characters in line" % linec

            And less readably,
            tempwords = line.split()
            for k in tempwords:
            linec = j
            if keyword.iskeywo rd(k):
            total = total + numwords(k)
            j = i + 1
            print" The word * %s * belongs in line number: %d" % (k,
            j)
            print "%i characters in line" % ( j - linec )

            Comment

            • James Stroud

              #7
              Re: Counting

              rockmode@gmail. com wrote:
              That's a short, abridged version of my code :) But, what I want is to
              count total# of keywords per line and print 'em. Rather than
              printing :
              >
              The word 'and' belongs in line num: 1
              The word 'del' belongs in line num: 1
              The word 'from' belongs in line num: 1
              >
              I want to print " Line #1 has 3 keywords"
              >
              ;)
              >

              I think it would be obvious how to write this:


              for i,line in enumerate(linel ist):
              line = line.split()
              for k in line:
              if keyword.iskeywo rd(k):
              c = line.count(k)
              total += line.count(k)
              print "Line #%d has %d keywords." % (i+1, c)
              break

              print "Total keyords are: %d" % total

              Comment

              • Andy

                #8
                Re: Counting

                I pretty doubt about this - "c = line.count(k)" I might wanna recheck
                on that.
                -------------------------------------
                I think it would be obvious how to write this:

                for i,line in enumerate(linel ist):
                line = line.split()
                for k in line:
                if keyword.iskeywo rd(k):
                c = line.count(k)
                total += line.count(k)
                print "Line #%d has %d keywords." % (i+1, c)
                break

                print "Total keyords are: %d" % total

                Comment

                • Andy

                  #9
                  Re: Counting

                  James -

                  I pretty doubt about this - "c = line.count(k)" You might wanna
                  recheck on that.
                  ------------------------------------------------
                  I think it would be obvious how to write this:

                  for i,line in enumerate(linel ist):
                  line = line.split()
                  for k in line:
                  if keyword.iskeywo rd(k):
                  c = line.count(k)
                  total += line.count(k)
                  print "Line #%d has %d keywords." % (i+1, c)
                  break

                  print "Total keyords are: %d" % total

                  >
                  I think it would be obvious how to write this:
                  >
                  for i,line in enumerate(linel ist):
                  line = line.split()
                  for k in line:
                  if keyword.iskeywo rd(k):
                  c = line.count(k)
                  total += line.count(k)
                  print "Line #%d has %d keywords." % (i+1, c)
                  break
                  >
                  print "Total keyords are: %d" % total

                  Comment

                  • John Machin

                    #10
                    Re: Counting

                    On 30/04/2007 7:17 AM, James Stroud wrote:
                    rockmode@gmail. com wrote:
                    >That's a short, abridged version of my code :) But, what I want is to
                    >count total# of keywords per line and print 'em. Rather than
                    >printing :
                    >>
                    >The word 'and' belongs in line num: 1
                    >The word 'del' belongs in line num: 1
                    >The word 'from' belongs in line num: 1
                    >>
                    >I want to print " Line #1 has 3 keywords"
                    >>
                    >;)
                    >>
                    >
                    >
                    I think it would be obvious how to write this:
                    >
                    >
                    for i,line in enumerate(linel ist):
                    line = line.split()
                    for k in line:
                    if keyword.iskeywo rd(k):
                    c = line.count(k)
                    total += line.count(k)
                    print "Line #%d has %d keywords." % (i+1, c)
                    break
                    >
                    print "Total keyords are: %d" % total
                    I would have thought so too. But the above is ... let's just say it's
                    not quite right. If there are 3 different keywords (as in the OP's
                    example), the above code prints 3 times for the same line.

                    Here's a straight-forward natural way to do it:
                    total = 0
                    for i, line in enumerate(linel ist):
                    c = 0
                    line = line.split()
                    for k in line:
                    if keyword.iskeywo rd(k):
                    c += 1
                    # Alternatively, replace above 5 lines by
                    # c = sum(keyword.isk eyword(k) for k in line.split())
                    # or the equivalent using map(), depending on taste etc :-)
                    total += c
                    print "Line #%d has %d keywords." % (i+1, c)
                    print "Total number of keywords is", total

                    ======

                    Perhaps someone should point out to the OP that using str.split as a
                    tokeniser is somewhat deficient:
                    1. comments and string literals could make the counts somewhat unreliable:
                    "# if not use mung(), will break while frobotzing later in code"
                    2. "else:"
                    3. "if not(0 <= n < maxn):"

                    HTH,
                    John

                    Comment

                    • Steve Holden

                      #11
                      Re: Counting

                      James Stroud wrote:
                      James Stroud wrote:
                      >James Stroud wrote:
                      [finally ...]
                      >
                      I should really wait until I've had some coffee. Not continue, but break!
                      >
                      >
                      for i,line in enumerate(linel ist):
                      line = line.split()
                      for k in line:
                      if keyword.iskeywo rd(k):
                      total += line.count(k)
                      print "The word '%s' belongs in line num: %d" % (k, i+1)
                      break
                      >
                      print "Total keyords are: %d" % total
                      James is actually trying to convince you of the merits of test-driven
                      development by cleverly showing you the disadvantages of not using it.

                      Clever approach to advocacy, James. Did you get your coffee yet?

                      regards
                      Steve
                      --
                      Steve Holden +1 571 484 6266 +1 800 494 3119
                      Holden Web LLC/Ltd http://www.holdenweb.com
                      Skype: holdenweb http://del.icio.us/steve.holden
                      ------------------ Asciimercial ---------------------
                      Get on the web: Blog, lens and tag your way to fame!!
                      holdenweb.blogs pot.com squidoo.com/pythonology
                      tagged items: del.icio.us/steve.holden/python
                      All these services currently offer free registration!
                      -------------- Thank You for Reading ----------------

                      Comment

                      • Andy

                        #12
                        Re: Counting

                        ..o0

                        Comment

                        Working...