473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

problem with dictionaries

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]

Jun 27 '08 #1
8 921
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

Jun 27 '08 #2
On Apr 23, 1:16*pm, Simon Strobl <Simon.Str...@gmail.comwrote:
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.
Jun 27 '08 #3
On Apr 23, 12:16 pm, Simon Strobl <Simon.Str...@gmail.comwrote:
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
Jun 27 '08 #4
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/

Jun 27 '08 #5
kdwyer a écrit :
On Apr 23, 12:16 pm, Simon Strobl <Simon.Str...@gmail.comwrote:
(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.

Jun 27 '08 #6
On Apr 23, 1:22 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
kdwyer a écrit :On Apr 23, 12:16 pm, Simon Strobl <Simon.Str...@gmail.comwrote:
(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
Jun 27 '08 #7
<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
Jun 27 '08 #8

"Simon Strobl" <Si**********@gmail.comwrote in message
news:ed**********************************@b64g2000 hsa.googlegroups.com...
| 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

Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for...
8
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
3
by: Shivram U | last post by:
Hi, I want to store dictionaries on disk. I had a look at a few modules like bsddb, shelve etc. However would it be possible for me to do the following hash = where the key is an int and not...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
2
by: David Pratt | last post by:
Hi. I like working with lists of dictionaries since order is preserved in a list when I want order and the dictionaries make it explicit what I have got inside them. I find this combination very...
3
by: Faisal Alquaddoomi | last post by:
Hello, I'm having a bit of trouble isolating my scripts from each other in my embedded Python interpreter, so that their global namespaces don't get all entangled. I've had some luck with...
8
by: placid | last post by:
Hi all, Just wondering if anyone knows how to pop up the dialog that windows pops up when copying/moving/deleting files from one directory to another, in python ? Cheers
2
by: gms | last post by:
Hello, I have the following list: My current list currently contains four dictionaries. They are: {'count': u'2', 'manu': <Manufacturer: Manu1>} {'count': u'4', 'manu': <Manufacturer:...
1
by: Edwin.Madari | last post by:
by the way, iterating over bar will throw KeyError if that key does not exist in foo. to see that in action, simply set another key in bar after copy.deepcopy stmt in this example.. bar = 0 and...
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.