473,395 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

sorting a dictionary?

I have a dictionary which contains some words and
their frequency in a short novel.

It looks like this:

mydict = {'the':358, 'they':29, 'went':7, 'said':65}

Is there an easy to sort this dictionary and get a
list like the following (in decreasing order)?

the 358
said 65
they 29
went 7

Thanks
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Jul 18 '05 #1
2 1370
This comes up so often on this list that there are many examples and
recipes.
You are looking for a dictionary sort by value.
search strategy:
Python sorting dictionary

44,000+ hits on google.
First entry:
http://aspn.activestate.com/ASPN/Pyt...k/Recipe/52306

hth,
M.E.Farmer

Jul 18 '05 #2
Anthony Liu wrote:
mydict = {'the':358, 'they':29, 'went':7, 'said':65}
Is there an easy to sort this dictionary and get a
list like the following (in decreasing order)?

Yes.

def sortkey((word, frequency)):
return -frequency, word

for word_freq in sorted(mydict.items(), key=sortkey):
print '%-6s %s' % word_freq
--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shalendra chhabra | last post by:
Hi, I just had a tryst with python. I was wondering if python is good enough to do this kind of job -- for it has extensive support of string and pattern matching, ordering and list handling. ...
4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
5
by: Rakesh | last post by:
Hi, For a particular problem of mine, I want to sort <key, value> pairs by its value. Eg: Input: A, 4 B, 5
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
1
by: sdunkerson | last post by:
I think this will be a good one for anyone who fancies themselves a wiz with manipulating data structure in vb.net. Imagine a Dictionary collection of Object "A". One of the properties of Object...
1
by: Sam Loxton | last post by:
Hi, I am fairly new to the python language and am trying to sort a nested Dictionary of a Dictionary which I wish to sort by value. The dictionary does not have to be restructured as I only need...
4
by: =?Utf-8?B?TUdSaWRlb3V0?= | last post by:
Hello, I have a SQL string that pulls data out of a database - I then calculate completerates based on Hours and # of Completes. I want to sort this data (FieldID and CompleteRates) by CompleteRate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.