...

count the item in a list without any numbers and quotations

SunJune
03-22-2011, 05:26 AM
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..



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?

!"£$%^&*()



: 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




['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']
[]
[]



#!/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)

shyam
03-22-2011, 09:16 AM
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])

SunJune
03-23-2011, 01:24 PM
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.


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum