problem with dictionaries

S

Simon Strobl

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

Peter Otten

Simon said:
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
 
C

Chris

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.
 
K

kdwyer

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
 
S

Steve Holden

Simon said:
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
 
B

Bruno Desthuilliers

kdwyer a écrit :
#!/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.
 
K

kdwyer

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

Simon Strobl

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
 
T

Terry Reedy

| 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top