Re: Sort the values of a dict

2009-12-19 Thread mattia
Il Sat, 19 Dec 2009 17:30:27 +1100, Lie Ryan ha scritto:

 On 12/19/2009 9:34 AM, mattia wrote:
 Can you provide me a much pythonic solution (with comments if possible,
 so I can actually learn something)?
 
 If you only need to get i'th element sometimes, sorting the dict is
 fine. Otherwise, you might want to use collections.OrderedDict.

Well, in the python doc OrderedDict is described as a dict that remembers 
the order that keys were first inserted and I don't need this. The fact 
is that I use a structure composed by a date and a list of possible 
solutions found, like
(2009/12/21, (('e', 12, 33), ('r', 4, 11), ('r', 1, 33))) then every 
solution is inserted concurrently in a dictionary. I want to sort the 
solution found to provide, e.g., the first 10 dates found and the best 
result of every date based on the i-th element of the date's list.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort the values of a dict

2009-12-19 Thread Rory Campbell-Lange
 mylist = [z for z in zip(list(d), list(d.values()))]
 The Pythonic way for the above line is:
  mylist = list(d.items())

Quite right. Thanks for pointing that out. 

I liked Chris Kaynor's solution:

s = sorted(d.items(), key=lambda i: i[1][2])

I'm a bit rusty, and have just picked up the new Learning Python book
but I'm finding that it isn't providing a useful refresher to the
language or a particularly good introduction to version 3. Have you any
suggestions.

Regards
Rory

-- 
Rory Campbell-Lange
r...@campbell-lange.net

Campbell-Lange Workshop
www.campbell-lange.net
0207 6311 555
3 Tottenham Street London W1T 2AF
Registered in England No. 04551928
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort the values of a dict

2009-12-19 Thread John Bokma
Rory Campbell-Lange r...@campbell-lange.net writes:

 I'm a bit rusty, and have just picked up the new Learning Python book
 but I'm finding that it isn't providing a useful refresher to the
 language or a particularly good introduction to version 3. Have you any
 suggestions.

While you aren't addressing me, I do have 2 suggestions:

 * Programming in Python 3 by Mark Summerfield (make sure you get the
   2nd edition)
 * Dive into Python 3 by Mark Pilgrim

I am halfway in the first book (but the 1st edition) and like it a
lot. I know Dive into Python from an earlier version (2.x) and have no
problem at all recommending the Python 3 edition. You can read/download it
here: http://diveintopython3.org/

-- 
John Bokma

Read my blog: http://johnbokma.com/
Hire me (Perl/Python): http://castleamber.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort the values of a dict

2009-12-19 Thread Rhodri James
On Fri, 18 Dec 2009 23:00:42 -, David Robinow drobi...@gmail.com  
wrote:



I won't engage in any arguments about pythonicity but it seems simpler
if you convert to a list of tuples right away.

d = {1:('a', 1, 12), 5:('r',21,10), 2:('u',9,8)}
l = [(x, d[x]) for x in d.keys()]


which is a long way of writing l = d.items(), or l = list(d.items())  
if you're using Python 3. :-)



def third(q):
return q[1][2]

s = sorted(l, key=third)
print s



--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sort the values of a dict

2009-12-18 Thread Chris Kaynor
I'd write it as:
s = sorted(d.iteritems(), key=lambda i: i[1][2])

If using python 3, it should be d.items() instead of d.iteritems().

d.iteritems() is a generator yielding tuples of (key, value) from
the dictionary 'd'.
lambda i: i[1][2] is the same as:
def sort_(i):
return i[1][2]
but in-line.

Chris


On Fri, Dec 18, 2009 at 2:34 PM, mattia ger...@gmail.com wrote:

 Hi all, I have a dictionary that uses dates and a tuples ad key, value
 pairs. I need to sort the values of the dict and insert everything in a
 tuple. The additional problem is that I need to sort the values looking
 at the i-th element of the list. I'm not that good at python (v3.1), but
 this is my solution:

  d = {1:('a', 1, 12), 5:('r', 21, 10), 2:('u', 9, 8)}
  t = [x for x in d.values()]
  def third(mls):
 ... return mls[2]
 ...
  s = sorted(t, key=third)
  pres = []
  for x in s:
 ... for k in d.keys():
 ... if d[k] == x:
 ... pres.append(k)
 ... break
 ...
  res = []
  for x in pres:
 ... res.append((x, d[x]))
 ...
  res
 [(2, ('u', 9, 8)), (5, ('r', 21, 10)), (1, ('a', 1, 12))]
 

 Can you provide me a much pythonic solution (with comments if possible,
 so I can actually learn something)?

 Thanks, Mattia
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort the values of a dict

2009-12-18 Thread mattia
Actually, in order to use duplicate values I need something like:

 import copy
 d = {1:('a', 1, 12), 5:('r', 21, 10), 2:('u', 9, 8), 3:('u', 9, 8) }
 dc = copy.deepcopy(d)
 t = [x for x in d.values()]
 def third(mls):
... return mls[2]
...
 s = sorted(t, key=third)
 pres = []
 for x in s:
... for k in d.keys():
... if d[k] == x:
... pres.append(k)
... del d[k] # speedup and use duplicate values
... break
...
 res = []
 for x in pres:
... res.append((x, dc[x]))
...
 res
[(2, ('u', 9, 8)), (3, ('u', 9, 8)), (5, ('r', 21, 10)), (1, ('a', 1, 
12))]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort the values of a dict

2009-12-18 Thread David Robinow
On Fri, Dec 18, 2009 at 5:34 PM, mattia ger...@gmail.com wrote:
 Hi all, I have a dictionary that uses dates and a tuples ad key, value
 pairs. I need to sort the values of the dict and insert everything in a
 tuple. The additional problem is that I need to sort the values looking
 at the i-th element of the list. I'm not that good at python (v3.1), but
 this is my solution:

 d = {1:('a', 1, 12), 5:('r', 21, 10), 2:('u', 9, 8)}
 t = [x for x in d.values()]
 def third(mls):
 ...     return mls[2]
 ...
 s = sorted(t, key=third)
 pres = []
 for x in s:
 ...     for k in d.keys():
 ...         if d[k] == x:
 ...             pres.append(k)
 ...             break
 ...
 res = []
 for x in pres:
 ...     res.append((x, d[x]))
 ...
 res
 [(2, ('u', 9, 8)), (5, ('r', 21, 10)), (1, ('a', 1, 12))]


 Can you provide me a much pythonic solution (with comments if possible,
 so I can actually learn something)?

 Thanks, Mattia
 --
 http://mail.python.org/mailman/listinfo/python-list

I won't engage in any arguments about pythonicity but it seems simpler
if you convert to a list of tuples right away.

d = {1:('a', 1, 12), 5:('r',21,10), 2:('u',9,8)}
l = [(x, d[x]) for x in d.keys()]
def third(q):
return q[1][2]

s = sorted(l, key=third)
print s
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort the values of a dict

2009-12-18 Thread mattia
Il Fri, 18 Dec 2009 18:00:42 -0500, David Robinow ha scritto:

 On Fri, Dec 18, 2009 at 5:34 PM, mattia ger...@gmail.com wrote:
 Hi all, I have a dictionary that uses dates and a tuples ad key, value
 pairs. I need to sort the values of the dict and insert everything in a
 tuple. The additional problem is that I need to sort the values looking
 at the i-th element of the list. I'm not that good at python (v3.1),
 but this is my solution:

 d = {1:('a', 1, 12), 5:('r', 21, 10), 2:('u', 9, 8)} t = [x for x in
 d.values()]
 def third(mls):
 ...     return mls[2]
 ...
 s = sorted(t, key=third)
 pres = []
 for x in s:
 ...     for k in d.keys():
 ...         if d[k] == x:
 ...             pres.append(k)
 ...             break
 ...
 res = []
 for x in pres:
 ...     res.append((x, d[x]))
 ...
 res
 [(2, ('u', 9, 8)), (5, ('r', 21, 10)), (1, ('a', 1, 12))]


 Can you provide me a much pythonic solution (with comments if possible,
 so I can actually learn something)?

 Thanks, Mattia
 --
 http://mail.python.org/mailman/listinfo/python-list

 I won't engage in any arguments about pythonicity but it seems simpler
 if you convert to a list of tuples right away.
 
 d = {1:('a', 1, 12), 5:('r',21,10), 2:('u',9,8)} l = [(x, d[x]) for x in
 d.keys()]
 def third(q):
 return q[1][2]
 
 s = sorted(l, key=third)
 print s

Thanks, I'm not yet aware of all the wonderful conversions python can do, 
amazing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort the values of a dict

2009-12-18 Thread Rory Campbell-Lange
On 18/12/09, mattia (ger...@gmail.com) wrote:
 Hi all, I have a dictionary that uses dates and a tuples ad key, value 
 pairs. I need to sort the values of the dict and insert everything in a 
 tuple. The additional problem is that I need to sort the values looking 
 at the i-th element of the list. I'm not that good at python (v3.1), but 
 this is my solution:
 
  d = {1:('a', 1, 12), 5:('r', 21, 10), 2:('u', 9, 8)}
  t = [x for x in d.values()]
  def third(mls):
 ... return mls[2]
 ...
  s = sorted(t, key=third)
  pres = []
  for x in s:
 ... for k in d.keys():
 ... if d[k] == x:
 ... pres.append(k)
 ... break
 ...
  res = []
  for x in pres:
 ... res.append((x, d[x]))
 ...
  res
 [(2, ('u', 9, 8)), (5, ('r', 21, 10)), (1, ('a', 1, 12))]

How about 

 mylist = [z for z in zip(list(d), list(d.values()))]

and then sort on your sort criteria, either i[0], i[0][1], i[0][2] or
i[0][3].

Rory



-- 
Rory Campbell-Lange
Director
r...@campbell-lange.net

Campbell-Lange Workshop
www.campbell-lange.net
0207 6311 555
3 Tottenham Street London W1T 2AF
Registered in England No. 04551928
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sort the values of a dict

2009-12-18 Thread MRAB

Rory Campbell-Lange wrote:

On 18/12/09, mattia (ger...@gmail.com) wrote:
Hi all, I have a dictionary that uses dates and a tuples ad key, value 
pairs. I need to sort the values of the dict and insert everything in a 
tuple. The additional problem is that I need to sort the values looking 
at the i-th element of the list. I'm not that good at python (v3.1), but 
this is my solution:



d = {1:('a', 1, 12), 5:('r', 21, 10), 2:('u', 9, 8)}
t = [x for x in d.values()]
def third(mls):

... return mls[2]
...

s = sorted(t, key=third)
pres = []
for x in s:

... for k in d.keys():
... if d[k] == x:
... pres.append(k)
... break
...

res = []
for x in pres:

... res.append((x, d[x]))
...

res

[(2, ('u', 9, 8)), (5, ('r', 21, 10)), (1, ('a', 1, 12))]


How about 


mylist = [z for z in zip(list(d), list(d.values()))]



The Pythonic way for the above line is:

 mylist = list(d.items())


and then sort on your sort criteria, either i[0], i[0][1], i[0][2] or
i[0][3].

Rory





--
http://mail.python.org/mailman/listinfo/python-list


Re: Sort the values of a dict

2009-12-18 Thread Lie Ryan

On 12/19/2009 9:34 AM, mattia wrote:

Can you provide me a much pythonic solution (with comments if possible,
so I can actually learn something)?


If you only need to get i'th element sometimes, sorting the dict is 
fine. Otherwise, you might want to use collections.OrderedDict.

--
http://mail.python.org/mailman/listinfo/python-list