Re: [Tutor] Sorting Dictionary of Dictionary by certain Value

2008-09-25 Thread Joe




Thanks to Kent Johnson, Robert Berman, Bill Campbell and John Fouhy for
the replies.
They have been useful.

Kent Johnson wrote:

  On Tue, Sep 23, 2008 at 8:41 PM, Joe Python [EMAIL PROTECTED] wrote:
  
  
Hi Pythonistas,

I have a large dictionary of dictionary (50,000+ keys) which has a structure
as follows:
DoD = {
'flintstones' : {
'husband'   : "fred",
'pal'   : "barney",
'income':  500,
},
'jetsons' : {
'husband'   : "george",
'wife'  : "jane",
'his boy' : "elroy",
'income':  700,
},
'simpsons' : {
'husband'   : "homer",
'wife'  : "marge",
'kid'   : "bart",
'income':  600,
},
};

I want to sort the dictionary by 'income'
Is there an efficient way to do the same.

  
  
As has been pointed out, you can't sort a dictionary, it is unordered.
You can sort the list of key, value pairs. The simplest way is to make
a key function that extracts the value on which to sort.

The key, value pairs will look like ('flintstones', {
'husband'   : "fred",
'pal'   : "barney",
'income':  500,
)

You want to sort on the 'income' element of the value; this key
function will work:
def key(item):
  return item[1]['income']

Then sort with
sorted(DoD.iteritems(), key=key)

Kent

  




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting Dictionary of Dictionary by certain Value

2008-09-24 Thread Hansen, Mike
 -Original Message-
 Hi Pythonistas,
 
 I have a large dictionary of dictionary (50,000+ keys) which 
 has a structure as follows:
 DoD = {
 'flintstones' : {
 'husband'   : fred,
 'pal'   : barney,
 'income':  500,
 },
 'jetsons' : {
 'husband'   : george,
 'wife'  : jane,
 'his boy' : elroy,
 'income':  700,
 },
 'simpsons' : {
 'husband'   : homer,
 'wife'  : marge,
 'kid'   : bart,
 'income':  600,
 },
 };
 
 I want to sort the dictionary by 'income'
 Is there an efficient way to do the same.
 Thanks in advance.
 
 - Jo
 

This sounds like it'd be a good job for a database...perhaps SQLite?

Mike
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting Dictionary of Dictionary by certain Value

2008-09-23 Thread John Fouhy
2008/9/24 Joe Python [EMAIL PROTECTED]:
 Hi Pythonistas,

 I have a large dictionary of dictionary (50,000+ keys) which has a structure
 as follows:
[snip]
 I want to sort the dictionary by 'income'
 Is there an efficient way to do the same.

Note that you cannot sort a dictionary.  The best you can do is build
a list containing the dictionary keys in the appropriate order and use
the dictionary in combination with that list.

You could try this:

1. Build a dictionary mapping income to list of families.
2. Sort keys to this dictionary.
3. Iterate through this sorted list, emitting family names.

e.g.

from collections import defaultdict
familiesByIncome = defaultdict(list)
for family in DoD:
  familiesByIncome[DoD[family]['income']].append(family)

incomes = familiesByIncome.keys()
incomes.sort() # sorts from lowest to highest

familiesSorted = []
for inc in incomes:
  familiesSorted.extend(familiesByIncome[inc])

##

HTH!

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting Dictionary of Dictionary by certain Value

2008-09-23 Thread Bill Campbell
On Wed, Sep 24, 2008, John Fouhy wrote:
2008/9/24 Joe Python [EMAIL PROTECTED]:
 Hi Pythonistas,

 I have a large dictionary of dictionary (50,000+ keys) which has a structure
 as follows:
[snip]
 I want to sort the dictionary by 'income'
 Is there an efficient way to do the same.

Note that you cannot sort a dictionary.  The best you can do is build
a list containing the dictionary keys in the appropriate order and use
the dictionary in combination with that list.

What you can do is create a class for the objects in the top
level dictionary, and create a __cmp__ method in that class using
cmp to compare the objects in the dictionary, finally sorting the
values.

class MyStuff(object):
def __init__(self, name, income):
self.name = name
self.income = int(income)
def cmp(self, other):
return(cmp((-self.income, self.name), -other.income, other.name))

d = dict(
key1 = MyStuff('john', 1),
key2 = MyStuff('bill', 2),
)

vals = d.values()
vals.sort()
# vals should be sorted by income in descending order and name.

Bill
--
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
Fax:(206) 232-9186

People who relieve others of their money with guns are called robbers. It
does not alter the immorality of the act when the income transfer is
carried out by government.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting Dictionary of Dictionary by certain Value

2008-09-23 Thread Bill Campbell
On Tue, Sep 23, 2008, Bill Campbell wrote:
On Wed, Sep 24, 2008, John Fouhy wrote:
2008/9/24 Joe Python [EMAIL PROTECTED]:
 Hi Pythonistas,

 I have a large dictionary of dictionary (50,000+ keys) which has a structure
 as follows:
[snip]
 I want to sort the dictionary by 'income'
 Is there an efficient way to do the same.

Note that you cannot sort a dictionary.  The best you can do is build
a list containing the dictionary keys in the appropriate order and use
the dictionary in combination with that list.

What you can do is create a class for the objects in the top
level dictionary, and create a __cmp__ method in that class using
cmp to compare the objects in the dictionary, finally sorting the
values.

class MyStuff(object):
def __init__(self, name, income):
self.name = name
self.income = int(income)

Whoops

The method below should be def __cmp__

def cmp(self, other):
return(cmp((-self.income, self.name), -other.income, other.name))

d = dict(
key1 = MyStuff('john', 1),
key2 = MyStuff('bill', 2),
)

vals = d.values()
vals.sort()
# vals should be sorted by income in descending order and name.

Bill
--
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
Fax:(206) 232-9186

People who relieve others of their money with guns are called robbers. It
does not alter the immorality of the act when the income transfer is
carried out by government.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


-- 
Bill
-- 
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
Fax:(206) 232-9186

Microsoft is to computers what Phillip Morris is to lungs.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting Dictionary of Dictionary by certain Value

2008-09-23 Thread Robert Berman




This might help you.

http://blog.modp.com/2008/09/sorting-python-dictionary-by-value-take.html


Robert

Joe Python wrote:

  Hi Pythonistas,
  
I have a large dictionary of dictionary (50,000+ keys) which has a
structure as follows:
DoD = {
 'flintstones' : {
 'husband' : "fred",
 'pal' : "barney",
 'income' : 500,
 },
 'jetsons' : {
 'husband' : "george",
 'wife' : "jane",
 'his boy' : "elroy",
 'income' : 700,
 },
 'simpsons' : {
 'husband' : "homer",
 'wife' : "marge",
 'kid' : "bart",
 'income' : 600,
 },
};
  
I want to sort the dictionary by 'income'
Is there an efficient way to do the same.
Thanks in advance.
  
- Jo
  
  

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
  



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting Dictionary of Dictionary by certain Value

2008-09-23 Thread Kent Johnson
On Tue, Sep 23, 2008 at 8:41 PM, Joe Python [EMAIL PROTECTED] wrote:
 Hi Pythonistas,

 I have a large dictionary of dictionary (50,000+ keys) which has a structure
 as follows:
 DoD = {
 'flintstones' : {
 'husband'   : fred,
 'pal'   : barney,
 'income':  500,
 },
 'jetsons' : {
 'husband'   : george,
 'wife'  : jane,
 'his boy' : elroy,
 'income':  700,
 },
 'simpsons' : {
 'husband'   : homer,
 'wife'  : marge,
 'kid'   : bart,
 'income':  600,
 },
 };

 I want to sort the dictionary by 'income'
 Is there an efficient way to do the same.

As has been pointed out, you can't sort a dictionary, it is unordered.
You can sort the list of key, value pairs. The simplest way is to make
a key function that extracts the value on which to sort.

The key, value pairs will look like ('flintstones', {
'husband'   : fred,
'pal'   : barney,
'income':  500,
)

You want to sort on the 'income' element of the value; this key
function will work:
def key(item):
  return item[1]['income']

Then sort with
sorted(DoD.iteritems(), key=key)

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor