help printing dictionary, sorted by values.

Thomas Wouters thomas at xs4all.nl
Tue Aug 17 02:58:25 EDT 1999


On Mon, Aug 16, 1999 at 11:16:47PM -0600, Jerry Williams wrote:

> I have searched and searched the docs and am really frustrated with
> them.
> All I can seem to find is bits and pieces that hint about dictionaries.
> I finally found a table under mappings that listed the methods.  But
> I couldn't find anything that would list each data type and what methods
> are available.  What I am trying to do is take a file with a bunch of
> entries that appear more than once and list the count of each occurance
> in order of the highest count.  So if the file looked like:
> a
> a
> a
> b
> b
> c
> Then is would print:
> 3 a
> 2 b
> 1 c

> I thought a dictionary would work great, but can't seem to figure out
> how to sort it by values and also get the key.
> list = dict.values()
> list.sort()

> Then I get lost.  And the counts aren't going to be unique.
> Thanks for any help.  Please point me to where the methods are listed
> for
> the dictionary.  And some examples of dictionaries would be helpful.

Have you tried the Python Tutorial ?
http://www.python.org/doc/current/tut/

As for examples, here's probably what you want:

[thomas at tilburg ~]$ python
Python 1.5.1 (#1, Sep  3 1998, 22:51:17)  [GCC 2.7.2.3] on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam

>>> dict = {} 		# an empty dictionary

>>> dict['spam'] = 'eggs' # Insert a string, with a string as key
>>> dict[5.4] = [2, 3, 4] # Insert a list, with a floating point number as key
>>> dict[1,2,3] = {} 	# insert an (empty) dictionary, with a tuple as key
>>> dict['me'] = dict 	# insert a reference to the dict itself

>>> dict		# 'print' the dict
{'spam': 'eggs', (1, 2, 3): {}, 'me': {...}, 5.4: [2, 3, 4]}
>>> dict.keys()		# print the dict's keys
['spam', (1, 2, 3), 'me', 5.4]
>>> dict.values()	# print the dict's values.
['eggs', {}, {'spam': 'eggs', (1, 2, 3): {}, 'me': {...}, 5.4: [2, 3, 4]}, [2, 3, 4]]

>>> dict['spam']	# Index by string
'eggs'
>>> myindx = 5.4	
>>> dict[myindx]	# Index by variable
[2, 3, 4]
>>> dict["me"]["me"][1,2,3]	# Index-index-index
{}

>>> for key in dict.keys():	# Print all dict values by hand
...     print dict[key]
... 
eggs
{}
{'spam': 'eggs', (1, 2, 3): {}, 'me': {...}, 5.4: [2, 3, 4]}
[2, 3, 4]

What you want to do is probably something like this:

import sys

if len(sys.argv) < 2:
	print "Usage: %s <file>"%sys.argv[0]
	sys.exit()

result = {}

file = open(sys.argv[1], 'r')
for line in file.readlines():
	line = line[:-1] # We want to lose the newline
	if result.has_key(line): # This could be done with exceptions too.
		result[line] = result[line] + 1
	else:
		result[line] = 1

resultlines = result.keys()
resultlines.sort(lambda x, y: cmp(result[y], result[x]))
for line in resultlines:
	print "%d %s"%(result[line], line)

Note cmp(result[y], result[x]), not cmp(result[x], result[y]), to sort the
list highest value first. You can also do a 'resultlines.reverse()' after
the sort to get the same effect.

This is what it does:

% python mysort.py testfile
66 d
60 f
36 l
36 ;
24 u
24 k
24 n
24 j
12 e
12 i
12 h
12 2
12 0
12 r
12 w
12 v
6 ,
4 s
3 p
2 a
1 m

Nevertheless, reading the tutorial and parts of the Library Reference really
are really good ideas.

and-i-didnt-even-show-the-other-10-good-ways-to-do-it'ly y'rs.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list