>>> 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
>>> 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))]
>>>
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.
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
ro...@campbell-lange.net
Campbell-Lange Workshop
www.campbell-lange.net
0207 6311 555
3 Tottenham Street London W1T 2AF
Registered in England No. 04551928
>>> mylist = list(d.items())
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.
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
> 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/
> 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