Mailing List Archive

help printing dictionary, sorted by values.
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.
help printing dictionary, sorted by values. [ In reply to ]
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@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@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
help printing dictionary, sorted by values. [ In reply to ]
Jerry Williams <jwilliam@xmission.com> 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.

How about:

def repr_sorted_dict(d):
"""Return a string representation of a dictionary,
sorted by value first."""
def cmpitems_by_value(a, b):
"""sort first by second element in sequence."""
v = cmp(a[1], b[1])
# if equal, then sort by first element in sequence
if v == 0:
v = cmp(a[0], b[0])
return v
import string
# retrieve the (key, value) pairs
items = d.items()
items.sort(cmpitems_by_value)
# display the pairs
l = []
for k, v in items:
l.append("%s: %s" % (`k`, `v`))
# now combine them as a dictionary display
return '{' + string.joinfields(l, ', ') + '}'

Then you can:
print repr_sorted_dict(my_values)

And if you really wanted to get fancy, you could make this function the
__repr__ method of a subclass of UserDict.UserDict.

from UserDict import UserDict
class ReverseSortedDict(UserDict):
__repr__ = lambda self: repr_sorted_dict(self.data)

(Although I'd rewrite it to use the UserDict interface rather than
self.data itself, but that's just me. :)

Even if this is not exactly what you want, the "pairs = dict.items();
pairs.sort(...)" should get you where you are going. :)

This is documented at:
http://www.python.org/doc/current/lib/typesmapping.html
http://www.python.org/doc/current/lib/typesseq-mutable.html

-Arcege