Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    count the item in a list without any numbers and quotations

    Hi Everyone,

    I have a file as below and want to have a output in the following format (second block). So far I have been able to get the result (please see third block). My code is in last block.

    As you can see I am still getting the numbers as well in my output, how can I get rid of that? I searched a lot, but couldnt able to get it.

    I still have to write the code for counting the items, but need to get this working first.

    Please advise..


    Code:
    Now
    is the time
    for all good-men
    to come to the-
    aid of -their
    party. NOW IS THE TIME FOR all Good men to
    come to the AID of their party. party-their now-is time-the-for all-good
    men to -come aid of party a&b%c*(d)
    a1b2c3d4-
    a
      b
     b  c   defg
    thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled?
    thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled2?
    thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled3?
    
    !"£$%^&*()
    Code:
    : 3
    aid: 3
    all: 3
    b: 4
    c: 3
    come: 3
    d: 2
    defg: 1
    for: 3
    good: 3
    is: 3
    men: 3
    now: 3
    of: 3
    party: 4
    the: 5
    their: 3
    thisisaverylongwordwithnospacesbetweenthewordsandhowwillitbehandled: 3
    time: 3
    to: 5
    Code:
    ['now']
    ['is', 'the', 'time']
    ['for', 'all', 'good', 'men']
    ['to', 'come', 'to', 'the']
    ['aid', 'of', 'their']
    ['party', 'now', 'is', 'the', 'time', 'for', 'all', 'good', 'men', 'to']
    ['come', 'to', 'the', 'aid', 'of', 'their', 'party', 'party', 'their', 'now', 'i
    s', 'time', 'the', 'for', 'all', 'good']
    ['men', 'to', 'come', 'aid', 'of', 'party', 'a', 'b', 'c', 'd']
    ['a1b2c3d4']
    ['a']
    ['b']
    ['b', 'c', 'defg']
    ['thisisaverylongwordwithnospacesbetweenthewordsandhowwillitbehandled']
    ['thisisaverylongwordwithnospacesbetweenthewordsandhowwillitbehandled2']
    ['thisisaverylongwordwithnospacesbetweenthewordsandhowwillitbehandled3']
    []
    []
    Code:
    #!/usr/bin/python
    import re
    
    fo=open("wordList.txt","r+")
    for line in fo:
       line=line.lower()
       #print(line,end='') 
       matches=re.findall(r'\w+',line)
       print(matches)

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Code:
    d = {}
    f = open('/tmp/in')
    for l in f:
      l = l.lower()
      m = re.findall(r'[a-z]+', l)
      for e in m:
        if d.get(e) == None:
          d[e] = 0
        d[e] = d[e] + 1
    
    l = d.keys()
    l.sort()
    for k in l:
      print '%s : %d' % (k, d[k])
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    SunJune (03-23-2011)

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks Shyam for the solution. I am now trying to print the output of this program to a file that is provided by the user at the command line.

    Below is the code that I am trying to run, but the problem is that it only generates an empty file. Can you please point out the discrepancy in the code.

    Code:
    class WriteObject:
      def __init__(self):
       self.content = []
      def write(self, string):
       self.content.append(string)
    
    g= open(sys.argv[2],'w+')
    g=WriteObject()
    sys.stdout = g
    sys.stdout = sys.__stdout__
    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •