problem with dictionaries

Discussion in 'Python' started by Simon Strobl, Apr 23, 2008.

  1. Simon Strobl

    Simon Strobl Guest

    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]
     
    Simon Strobl, Apr 23, 2008
    #1
    1. Advertising

  2. Simon Strobl

    Peter Otten Guest

    Simon Strobl wrote:

    > 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?


    The code looks OK. You probably have python reading a my_frqlist.txt that
    differs from the one you are looking at.

    Peter
     
    Peter Otten, Apr 23, 2008
    #2
    1. Advertising

  3. Simon Strobl

    Chris Guest

    On Apr 23, 1:16 pm, Simon Strobl <> 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]


    there's nothing wrong with that section, just try this when building
    your dictionary...

    if word in frq:
    print 'Duplicated Word Found: %s' % word

    only possibility that I can think of, and that is logical, is that you
    have duplicate keys in your file.
     
    Chris, Apr 23, 2008
    #3
  4. Simon Strobl

    kdwyer Guest

    On Apr 23, 12:16 pm, Simon Strobl <> 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
     
    kdwyer, Apr 23, 2008
    #4
  5. Simon Strobl

    Steve Holden Guest

    Simon Strobl 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]
    >

    <flippancy>You musts have missed the memo. The rules of the universe
    changed at 0834 UST yesterday, and all functioning Python programs
    stopped working.</flippancy>

    More seriously, *something* must have changed - it's probably not the
    rules of the universe though. Are the files now coming from a different
    source (Windows rather than Unix or vice versa)?

    As you read in the data, insert a

    print "%r: %s %s" % (line, frequency, word)

    to see exactly what is being processed and how it is getting split.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Apr 23, 2008
    #5
  6. kdwyer a écrit :
    > On Apr 23, 12:16 pm, Simon Strobl <> wrote:

    (snip)
    >> #!/usr/bin/python
    >>
    >> import sys
    >>
    >> frqlist = open('my_frqlist.txt', 'r')

    (snip)
    >> frq = {}
    >>
    >> for line in frqlist:
    >> line = line.rstrip()
    >> frequency, word = line.split('|')
    >> frq[word] = int(frequency)
    >>

    (snip)

    > It works for me, save that you need to read the file into a list first


    You don't, unless you're using an old python versions (I'd say 2.3 or
    older). Files are now their own iterators.
     
    Bruno Desthuilliers, Apr 23, 2008
    #6
  7. Simon Strobl

    kdwyer Guest

    On Apr 23, 1:22 pm, Bruno Desthuilliers <bruno.
    > wrote:
    > kdwyer a écrit :> On Apr 23, 12:16 pm, Simon Strobl <> wrote:
    > (snip)
    > >> #!/usr/bin/python

    >
    > >> import sys

    >
    > >> frqlist = open('my_frqlist.txt', 'r')

    > (snip)
    > >> frq = {}

    >
    > >> for line in frqlist:
    > >> line = line.rstrip()
    > >> frequency, word = line.split('|')
    > >> frq[word] = int(frequency)

    >
    > (snip)
    >
    > > It works for me, save that you need to read the file into a list first

    >
    > You don't, unless you're using an old python versions (I'd say 2.3 or
    > older). Files are now their own iterators.


    *Fiddles with the interpreter for a moment*
    So they are - I'd quite forgotten about that - thanks for the
    reminder!

    K
     
    kdwyer, Apr 23, 2008
    #7
  8. Simon Strobl

    Simon Strobl Guest

    > <flippancy>You musts have missed the memo. The rules of the universe
    > changed at 0834 UST yesterday, and all functioning Python programs
    > stopped working.</flippancy>


    As always, the rules of the universe have not changed. (Or, at least,
    I do hope so.)

    It seems that the cause of my problem was my switching too fast
    between too many and too seldom saved emacs buffers.

    Thanks to all for your hints.

    Simon
     
    Simon Strobl, Apr 23, 2008
    #8
  9. Simon Strobl

    Terry Reedy Guest

    "Simon Strobl" <> wrote in message
    news:...
    | Any hints?

    For future reference, I would consider using sample data in the file itself
    for debugging -- to separate an algorithm problem from a file problem:

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

    Change above to:

    # frqlist = open('my_frqlist.txt', 'r')

    frqlist = '''\
    787560608|the
    434879575|of
    413442185|and
    395209748|to
    284833918|a
    249111541|in
    169988976|is'''.split('\n') # sample my_frqlist.txt

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

    An alternative is 'print line' in the first loop after stripping.

    tjr
     
    Terry Reedy, Apr 23, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    0
    Views:
    422
  2. Replies:
    6
    Views:
    336
    Steven Bethard
    Jan 31, 2005
  3. ZMY
    Replies:
    3
    Views:
    260
    Robert Kern
    Apr 24, 2007
  4. lysdexia
    Replies:
    6
    Views:
    561
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    517
    Brandon
    Aug 15, 2008
Loading...

Share This Page