Sorting distionary by value

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Mar 22 09:24:09 EST 2002


Artur Skura <arturs at iidea.pl> wrote in 
news:slrna9m5sc.ai9.arturs at aph.waw.pdi.net:

> known = []
> times = 0
> output = {}
> 
> for i in a:
>     if i not in known:
As the 'known' list gets longer, the check for list membership gets slower. 
A much better way is to keep a dictionary of the words (which you already 
have) and check for dictionary membership:
      if not output.has_key(i):

>         for l in a:
>             if l == i:
>                 times = times + 1
And this bit means that you loop through all your input again and again: 
once for every unique word. Better just to go once through the data and add 
1 to the count each time.

>         known.append(i)
>         output[i] = times
>     times = 0
> 
<snip>
> it seems it's slow not because of sorting...

Try something like:
for i in a:
    	if output.has_key(i):
    	    	output[i] += 1
     else:
    	    	output[i] = 1

Also put your code inside a function to shave a little bit more off the 
runtime. 

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list