Re: Python Dict problems
From: Max M (maxm_at_mxm.dk)
Date: 02/03/04
- Next message: Gerrit Holl: "Re: Variable scope within a class"
- Previous message: Nuff Said: "Re: Variable scope within a class"
- In reply to: venu gopal: "Python Dict problems"
- Next in thread: James Henderson: "Re: Python Dict problems"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 03 Feb 2004 11:57:15 +0100 To: venu gopal <madhuananth@hotmail.com>
venu gopal wrote:
> iam finding great difficulty in handling a two-dimentional dictionary or
> any other similar data structure for the following purpose:
> from a collection of words,i need to have the count of each word so if i
> use:
> str=f.getline()
> for l in range(flen):
> if subs[l].has_key(str):
> subs[l][str]=1
> else:
> subs[l][str]+=1
If it is a smalish file::
content = f.read()
result = {}
for word in words:
result[word] = content.count(word)
For big files::
result = {}
for l in f:
for word in words:
result[word] = result.get(word, 0) + l.count(word)
There are probably faster methods using re, where the counting is done
by the reg-ex engine.
regards Max M
- Next message: Gerrit Holl: "Re: Variable scope within a class"
- Previous message: Nuff Said: "Re: Variable scope within a class"
- In reply to: venu gopal: "Python Dict problems"
- Next in thread: James Henderson: "Re: Python Dict problems"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|