Re: Python Dict problems

From: Max M (maxm_at_mxm.dk)
Date: 02/03/04


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



Relevant Pages

  • Re: How to: referencing variables using the contents of otehr variables.
    ... the overhead would be less than the noise threshold. ... But with regards to enums in particular, That did go a little beyond ... I'll probably end up giving enums ... of my current data structure before I try a different one. ...
    (comp.lang.java.help)
  • Re: software engineering, program construction
    ... why make a copy of the data structure to ... are passing the actual data behind the back in global variables. ... purpose, a special purpose tool as it were, and not a general purpose ... then why not practise those everywhere. ...
    (comp.lang.perl.misc)
  • Re: [Full-Disclosure] Akamai
    ... IIRC the main purpose of DoS attacks (apart from kiddie fights) ... Regards, ...
    (Full-Disclosure)
  • Re: OT: Spring time change , 1 hr ahead
    ... I hate this and to add insult it serves absolutely no purpose. ... Regards, ...
    (alt.smokers.pipes)
  • Re: ANSI C Newbie Question
    ... >>NAVIA C is despised because I am just a nobody. ... > purpose of bringing the group into their way of thinking with ... > regards to C extensions. ...
    (comp.lang.c)