problem with dictionaries

kdwyer kevin.p.dwyer at gmail.com
Wed Apr 23 08:04:38 EDT 2008


On Apr 23, 12:16 pm, Simon Strobl <Simon.Str... at gmail.com> wrote:
> Hello,
>
> the idea of the following program is to parse a frequency list of the
> form FREQUENCY|WORD, to store the frequency of a word in a dictionary
> (and to do some things with this information later).
>
> I have done this many many times. Suddenly, it does not work any more:
> The value frq[key] is different from the value that key has in the
> file 'my_frqlist.txt'.
>
> I am using Python 2.5.1
>
> Any hints?
>
> Simon
>
> ================================================
>
> #!/usr/bin/python
>
> import sys
>
> frqlist = open('my_frqlist.txt', 'r')
>
> # my_frqlist looks like this:
> # 787560608|the
> # 434879575|of
> # 413442185|and
> # 395209748|to
> # 284833918|a
> # 249111541|in
> # 169988976|is
>
> frq = {}
>
> for line in frqlist:
>     line = line.rstrip()
>     frequency, word = line.split('|')
>     frq[word] = int(frequency)
>
> for key in frq.keys():
>     print key,  frq[key]

It works for me, save that you need to read the file into a list first
- is this really the code that you are running?  What results are you
getting?

K



More information about the Python-list mailing list