Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old October 1st, 2002, 09:47 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
sorting by value

Let's say I want to build a map, where the key is the file name and the value is the size in bytes:

files["foo"] = 12345

Once it is built, I want to sort by the file size, largest first. In Perl, I could do this:

foreach my $key (sort { $files{$b} <=> $files{$a} } keys %files)

How do I do this in python??

Reply With Quote
  #2  
Old October 1st, 2002, 10:10 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
I came up with a solution, but it may not be the best way. Opinions?

--------------
def sort_byval(dict, reverse=0):
if type(dict) is not type({}): return []
keys = dict.keys()

s = map(lambda k: (dict[k], k), keys)
s.sort()
if reverse: s.reverse()

return s
# end sort_byval

m = { 'a': 1000, 'b': 2000, 'c': 3000 }
v = sort_byval(m,1)

for v1,v2 in v: print v1

---------------------

prints:
3000
2000
1000

Reply With Quote
  #3  
Old October 3rd, 2002, 04:42 PM
ZeUs's Avatar
ZeUs ZeUs is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: St. George, Utah
Posts: 63 ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level)ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level)ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level)ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level)ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level)ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 31
I can't think of a better way to do it really, but I'd like to propose a change to is so it returns a sorted dictionary instead of a list of (val, key) tuples.

Code:
def sort_byval(dict, reverse=0):
  if type(dict) is not type({}): return []

  keys = dict.keys()
  s = map(lambda k: (dict[k], k), keys)
  s.sort()

  if reverse: s.reverse()
  
  d = {}
  for item in s:
    d[item[1]] = item[0]

  return d
  
  m = {'a':1000, 'b':4000, 'c':2000}
  v = sort_byval(m)


v would now contain:
Code:
{'a': 1000, 'c': 2000, 'b': 4000}


Which is the dictionary passed, sorted by value.
__________________
Lucas Marshall

Reply With Quote
  #4  
Old October 3rd, 2002, 07:45 PM
moramis moramis is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 1 moramis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry, ZeUs, but dictionaries are always unsorted. You cannot rebuild a dictionary in a different order and expect it to remain that way. In fact, after a call "v = sort_byval(m)" v and m will be identical copies of each other.

Either stick with vpopper's solution or, if you really must have a dictionary, look for sorted dictionary implementations on the Vaults of Pernassus.

Reply With Quote
  #5  
Old October 4th, 2002, 10:32 AM
ZeUs's Avatar
ZeUs ZeUs is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: St. George, Utah
Posts: 63 ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level)ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level)ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level)ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level)ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level)ZeUs User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 31
Duh.... I knew that...

See what happens if you don't pay attention?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > sorting by value


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway