0

I have the following dictionary:

DICT = {'ABC': [[u'2012-07-19', 1.22, 1, 1.22, 1, 0], [u'2012-04-26', 1.33, 210, 1.33, 210, 0], [u'2011-11-14', 1.25, 171, 1.32, 241, 70]], 
'XYZ': [[u'2012-02-17', 1.82, 59, 1.25, 182, 123], [u'2011-04-06', 1.50, 30, 1.18, 54, 24], [u'2011-01-06', 1.21, 38, 1.17, 41, 3], [u'2010-06-21', 1.28, 26, 1.28, 26, 0]]}

Values are two dimensional list.

How can I sort keys based on the third column of values.

Let me explain: The third column minimum value of key 'ABC' is 1.22 (among 1.22,1.33 and 1.32). The third column minimum value of key 'XYZ' is 1.17 (among 1.25,1.18,1.17 and 1.28). Since 1.17 > 1.22 'XYZ' will be first and then 'ABC'.

4
  • Are you trying to sort the dictionary? If so, that is impossible on the account of being represented most likely as a binary tree, thereby not being sortable. Commented Aug 1, 2012 at 1:29
  • Your code example appears as though it will be very difficult for others to read or maintain. I highly recommend creating a class, in this case. Commented Aug 1, 2012 at 1:35
  • @Nino: What do you mean? Commented Aug 1, 2012 at 2:46
  • @kojiro Thanks for your answer. I was trying with itemgetter in the itertools module Commented Aug 1, 2012 at 9:58

1 Answer 1

2

You can use the python builtin sorted with a key function to pick out the specific characteristic of the iterable you want to use as a sort key:

>>> [l[1] for l in DICT['XYZ']]
[1.82, 1.5, 1.21, 1.28]
>>> min(l[1] for l in DICT['ABC'])
1.22
>>> sorted(DICT, key=lambda k: min(l[1] for l in DICT[k]))
['XYZ', 'ABC']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.