Re: problem with dictionaries
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with dictionaries
On Apr 23, 1:16 pm, Simon Strobl [EMAIL PROTECTED] 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with dictionaries
On Apr 23, 12:16 pm, Simon Strobl [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with dictionaries
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] flippancyYou 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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with dictionaries
kdwyer a écrit : On Apr 23, 12:16 pm, Simon Strobl [EMAIL PROTECTED] 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with dictionaries
On Apr 23, 1:22 pm, Bruno Desthuilliers bruno. [EMAIL PROTECTED] wrote: kdwyer a écrit : On Apr 23, 12:16 pm, Simon Strobl [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with dictionaries
flippancyYou 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with dictionaries
Simon Strobl [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem in Dictionaries
Glauco Silva [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I would like to know if the dictionary can sort with a function that i give to then! No. You have to either make a list of key,value items and sort that, possibly with a cmp or key parameter items = d.items() items.sort(() or write or find a sorted_dict class (Google may help). In most applications, people find that they only need a sorted listing of a dict occasionally, so they just use the fast builtin dict and the fast builtin list sort when needed. Terry J. Reedy -- http://mail.python.org/mailman/listinfo/python-list