[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> </DIV>
<DIV>Value key frequency positions where the word was found</DIV>
<DIV><STRONG>natural doc1.txt (89, (17, 22, 26))</STRONG></DIV>
<DIV> </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> </DIV>
<DIV>def SortDict(adict):<BR> """ this function sorts the frequency/key pairs in the<BR> dictionary by highest to lowest frequency """<BR> counts = {}<BR> for word in adict.items():<BR> if counts.has_key(word):<BR> counts[word] += 1<BR> else:<BR> <BR> counts[word] = 1<BR> <BR> keys = counts.keys()<BR> keys.sort(lambda x, y: cmp(counts[y], counts[x]))<BR> print"\n--------------------------------------------"<BR> </DIV>
<DIV> #display the top five elements<BR> for i in range(5):<BR> print keys + "\t" + counts[keys[i]]<BR> <BR> print "\n\n"<BR> return keys </DIV>
<DIV> </DIV>
<DIV>having implemented this way it is giving me a syntax error which is as follws:</DIV>
<DIV> </DIV>
<DIV> File "C:\Python22\Ass2Test\SubIndex.py", line 9, in SortDict<BR> if counts.has_key(word):<BR>TypeError: list objects are unhashable</DIV>
<DIV> </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> </DIV>
<DIV>thanks in advance</DIV>
<DIV> </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--