467,923 Members | 1,244 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Dictionary sorting problem

Hi,
I have a dictionary for counting ocurrences of strings in a document.
The dictionary looks like this:

'hello':135
'goodbye':30
'lucy':4
'sky':55
'diamonds':239843
'yesterday':4

I want to print the dictionary so I see most common words first:

'diamonds':239843
'hello':135
'sky':55
'goodbye':30
'lucy':4
'yesterday':4

How do I do this? Notice I can't 'swap' the dictionary (making keys
values and values keys) and sort because I have values like lucy &
yesterday which have the same number of occurrences.

Thanks.

Sep 16 '05 #1
  • viewed: 2096
Share:
5 Replies
JerryB wrote:
Hi,
I have a dictionary for counting ocurrences of strings in a document.
The dictionary looks like this:

'hello':135
'goodbye':30
'lucy':4
'sky':55
'diamonds':239843
'yesterday':4

I want to print the dictionary so I see most common words first:

'diamonds':239843
'hello':135
'sky':55
'goodbye':30
'lucy':4
'yesterday':4

How do I do this? Notice I can't 'swap' the dictionary (making keys
values and values keys) and sort because I have values like lucy &
yesterday which have the same number of occurrences.

Thanks.


Don't try to 'swap' the dict, just sort a list based on the items in
the dict. Try this:

original= {
'hello':135,
'goodbye':30,
'lucy':4,
'sky':55,
'diamonds':239843,
'yesterday':4 }

items = sorted( (v,k) for (k,v) in original.iteritems() )
items.reverse() # depending on what order you want
print items
The result is:
[(239843, 'diamonds'), (135, 'hello'), (55, 'sky'), (30, 'goodbye'), (4, 'yesterday'),
(4, 'lucy')]
--Irmen
Sep 16 '05 #2
You can't sort dictionaries (as implemented by hash tables), they are
unordered data types, so by definition there's no way to force an order
on them.

http://en.wikipedia.org/wiki/Hash_tables

Sep 16 '05 #3
On Fri, 16 Sep 2005 21:42:40 +0200, Irmen de Jong <ir**********@xs4all.nl> wrote:
JerryB wrote:
Hi,
I have a dictionary for counting ocurrences of strings in a document.
The dictionary looks like this:

'hello':135
'goodbye':30
'lucy':4
'sky':55
'diamonds':239843
'yesterday':4

I want to print the dictionary so I see most common words first:

'diamonds':239843
'hello':135
'sky':55
'goodbye':30
'lucy':4
'yesterday':4

How do I do this? Notice I can't 'swap' the dictionary (making keys
values and values keys) and sort because I have values like lucy &
yesterday which have the same number of occurrences.

Thanks.


Don't try to 'swap' the dict, just sort a list based on the items in
the dict. Try this:

original= {
'hello':135,
'goodbye':30,
'lucy':4,
'sky':55,
'diamonds':239843,
'yesterday':4 }

items = sorted( (v,k) for (k,v) in original.iteritems() )
items.reverse() # depending on what order you want
print items
The result is:
[(239843, 'diamonds'), (135, 'hello'), (55, 'sky'), (30, 'goodbye'), (4, 'yesterday'),
(4, 'lucy')]

or tell sorted what to do ;-)
original= { ... 'hello':135,
... 'goodbye':30,
... 'lucy':4,
... 'sky':55,
... 'diamonds':239843,
... 'yesterday':4 } list(sorted(original.iteritems(), None, lambda t:t[1], True))

[('diamonds', 239843), ('hello', 135), ('sky', 55), ('goodbye', 30), ('yesterday', 4), ('lucy',4)]

Regards,
Bengt Richter
Sep 17 '05 #4
Bengt Richter wrote:
or tell sorted what to do ;-)
original= { ... 'hello':135,
... 'goodbye':30,
... 'lucy':4,
... 'sky':55,
... 'diamonds':239843,
... 'yesterday':4 } list(sorted(original.iteritems(), None, lambda t:t[1], True)) [('diamonds', 239843), ('hello', 135), ('sky', 55), ('goodbye', 30),
('yesterday', 4), ('lucy',4)]


or a slight variation on this theme which just gives you the keys in value
order rather than the tuples:
for k in sorted(original, key=original.get, reverse=True):

print k, original[k]
diamonds 239843
hello 135
sky 55
goodbye 30
yesterday 4
lucy 4
Sep 17 '05 #5
On 17 Sep 2005 11:01:41 GMT, Duncan Booth <du**********@invalid.invalid> wrote:
Bengt Richter wrote:
or tell sorted what to do ;-)
>>> original= { ... 'hello':135,
... 'goodbye':30,
... 'lucy':4,
... 'sky':55,
... 'diamonds':239843,
... 'yesterday':4 }
>>> list(sorted(original.iteritems(), None, lambda t:t[1], True))

[('diamonds', 239843), ('hello', 135), ('sky', 55), ('goodbye', 30),
('yesterday', 4), ('lucy',4)]


or a slight variation on this theme which just gives you the keys in value
order rather than the tuples:
for k in sorted(original, key=original.get, reverse=True):

print k, original[k]

Nice. I like the keyword usage too. Much clearer than my hastypaste ;-)

diamonds 239843
hello 135
sky 55
goodbye 30
yesterday 4
lucy 4


Regards,
Bengt Richter
Sep 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jocknerd | last post: by
4 posts views Thread by Rory Campbell-Lange | last post: by
2 posts views Thread by Anthony Liu | last post: by
9 posts views Thread by Odd-R. | last post: by
1 post views Thread by Sam Loxton | last post: by
5 posts views Thread by Martin Pöpping | last post: by
5 posts views Thread by =?Utf-8?B?THVpZ2k=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.