[Tutor] help for dictionary sorting program

Abdirizak abdi a_abdi406@yahoo.com
Fri Apr 18 09:46:38 2003


--0-2098158533-1050673517=:39089
Content-Type: text/plain; charset=us-ascii

Hi,I was working on a function that takes a adictionary as an argument and returns the frequency ordering of elements in the dictionary. I have dictionary set up as follows: Value         key        frequency   positions where the word was foundnatural    doc1.txt   (89,           (17, 22, 26)) the last 3 numbers means the word is found position 17,22,26, and the frequency of that word is 89, the word found is natural and it is from afile called doc1.txt.my intention is to pick-up the items in the dictionary by frequency and Iimplemented the function this way: def SortDict(adict):
        """ this function sorts the frequency/key pairs in the
            dictionary by highest to lowest frequency   """
        counts = {}
        for word in adict.items():
                if counts.has_key(word):
                        counts[word] += 1
                else:
                        
                        counts[word] = 1
            
        keys = counts.keys()
        keys.sort(lambda x, y: cmp(counts[y], counts[x]))
        print"\n--------------------------------------------"
              #display the top five elements
        for i in range(5):
                print keys + "\t" + counts[keys[i]]
                
        print "\n\n"
        return keys    having implemented this way it is giving me a syntax error which is as follws:  File "C:\Python22\Ass2Test\SubIndex.py", line 9, in SortDict
    if counts.has_key(word):
TypeError: list objects are unhashable can anyone help me figure out what I am getting wrong ? or suggestany way that I can achieve my objectives. thanks in advance 


---------------------------------
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
--0-2098158533-1050673517=:39089
Content-Type: text/html; charset=us-ascii

<DIV>Hi,</DIV>
<DIV>I was working on a function that takes a adictionary as an argument and returns the frequency ordering of elements in the dictionary. I have dictionary set up as follows:</DIV>
<DIV>&nbsp;</DIV>
<DIV>Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frequency&nbsp;&nbsp; positions where the word was found</DIV>
<DIV><STRONG>natural&nbsp;&nbsp;&nbsp; doc1.txt&nbsp;&nbsp; (89,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  (17, 22, 26))</STRONG></DIV>
<DIV>&nbsp;</DIV>
<DIV>the last 3 numbers means the word is found position <STRONG>17,22,26</STRONG>, and the frequency of that word is <STRONG>89, </STRONG>the word found is <STRONG>natural </STRONG>and it is from afile called <STRONG>doc1.txt.</STRONG></DIV>
<DIV>my intention is to pick-up the items in the dictionary by frequency and I</DIV>
<DIV>implemented the function this way:</DIV>
<DIV>&nbsp;</DIV>
<DIV>def SortDict(adict):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """ this function sorts the frequency/key pairs in the<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dictionary by highest to lowest frequency&nbsp;&nbsp; """<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; counts = {}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for word in adict.items():<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if counts.has_key(word):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; counts[word] += 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; counts[word] = 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keys = counts.keys()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keys.sort(lambda x, y: cmp(counts[y], counts[x]))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print"\n--------------------------------------------"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #display the top five elements<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for i in range(5):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print keys + "\t" + counts[keys[i]]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "\n\n"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return keys&nbsp;&nbsp; </DIV>
<DIV>&nbsp;</DIV>
<DIV>having implemented this way it is giving me a syntax error which is as follws:</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;File "C:\Python22\Ass2Test\SubIndex.py", line 9, in SortDict<BR>&nbsp;&nbsp;&nbsp; if counts.has_key(word):<BR>TypeError: list objects are unhashable</DIV>
<DIV>&nbsp;</DIV>
<DIV>can anyone help me figure out what I am getting wrong ? or suggest</DIV>
<DIV>any way that I can achieve my objectives.</DIV>
<DIV>&nbsp;</DIV>
<DIV>thanks in advance</DIV>
<DIV>&nbsp;</DIV><p><br><hr size=1>Do you Yahoo!?<br>
<a href="http://us.rd.yahoo.com/search/mailsig/*http://search.yahoo.com">The New Yahoo! Search</a> - Faster. Easier. Bingo.
--0-2098158533-1050673517=:39089--