Dictionary sorting problem

J

JerryB

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.
 
I

Irmen de Jong

JerryB said:
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
 
B

Bengt Richter

JerryB said:
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 ;-)
... '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
 
D

Duncan Booth

Bengt said:
or tell sorted what to do ;-)
... '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:
print k, original[k]


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

Bengt Richter

Bengt said:
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:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top