-3

I have list of dictionaries:

l_dict = [{'start': 2, 'length': 2, 'end': 4}, {'start': 1, 'length': 5, 'end': 8}, {'start': 3, 'length': 2, 'end': 7}, {'start': 3, 'length': 1, 'end': 3}, {'start': 2, 'length': 1, 'end': 2}, {'start': 1, 'length': 4, 'end': 5}, {'start': 1, 'length': 5, 'end': 9}, {'start': 3, 'length': 1, 'end': 3}, {'start': 1, 'length': 5, 'end': 6}, {'start': 1, 'length': 3, 'end': 3}, {'start': 1, 'length': 2, 'end': 2}, {'start': 1, 'length': 3, 'end': 5}, {'start': 4, 'length': 1, 'end': 4}, {'start': 1, 'length': 4, 'end': 4}, {'start': 1, 'length': 1, 'end': 1}, {'start': 1, 'length': 4, 'end': 6}]

How can I find the dict in the list with greatest 'length' and minimum 'start' and maximum 'end' entry?

Patrick Yu
  • 972
  • 1
  • 7
  • 19
Will
  • 23
  • 6
  • i just tried min or max (l_dict, key=lambda x:x[key]) i don't have an idea how to combine the three – Will Nov 08 '15 at 04:14
  • Well I don't have any idea what you mean by "combine the three". Seems to me you're looking for three separate bits of information. If that's not correct, you need to revise your Q to clarify what you're actually trying to do, and like, what is the expected/correct output if given this list of dict. – David Zemens Nov 08 '15 at 04:16
  • the output need to be dict with minimum start value and maximum end value and greater length that is {'start': 1, 'length': 5, 'end': 9} – Will Nov 08 '15 at 04:17
  • What would be the output of `[{'start': 1, 'length': 5, 'end': 9}, {'start': 1, 'length': 6, 'end': 8}`? If start/length/end are not related in such a way like (end == start+length) that you can discard one of the criteria, I don't know how this is going to possible, because you have three criteria for three independent variables, each of which can conflict with others: rock beats scissors but scissors beats paper but paper beats rock... – David Zemens Nov 08 '15 at 04:27
  • @DavidZemens u r right :) – Will Nov 08 '15 at 04:38
  • Possible duplicate of [Get key with the least value from a dictionary](http://stackoverflow.com/questions/3282823/get-key-with-the-least-value-from-a-dictionary) – Bill the Lizard Apr 05 '17 at 14:16

2 Answers2

1

If you want you can sort by the properties you need and take the first element.

l_dict = [{'start': 2, 'length': 2, 'end': 4}, {'start': 1, 'length': 5, 'end': 8}, {'start': 3, 'length': 2, 'end': 7}, {'start': 3, 'length': 1, 'end': 3}, {'start': 2, 'length': 1, 'end': 2}, {'start': 1, 'length': 4, 'end': 5}, {'start': 1, 'length': 5, 'end': 9}, {'start': 3, 'length': 1, 'end': 3}, {'start': 1, 'length': 5, 'end': 6}, {'start': 1, 'length': 3, 'end': 3}, {'start': 1, 'length': 2, 'end': 2}, {'start': 1, 'length': 3, 'end': 5}, {'start': 4, 'length': 1, 'end': 4}, {'start': 1, 'length': 4, 'end': 4}, {'start': 1, 'length': 1, 'end': 1}, {'start': 1, 'length': 4, 'end': 6}]
l_dict_sorted = sorted(l_dict, key=lambda x: (-x['length'],x['start'],-x['end']))
l_dict_sorted
Luca Fiaschi
  • 3,145
  • 7
  • 31
  • 44
  • 1
    huge amount of data is there, so i just want a better way to reduce loading time,any suggestions ? – Will Nov 08 '15 at 04:21
  • you should say in which format is your data initially stored beside that maximum is a linear operation while sorting is a n*log(n) operation – Luca Fiaschi Nov 08 '15 at 04:23
1

Using generators to do this is preferable... And I am throwing in a lambda as they are excellent for one liners.

getExtremum = lambda seq, attr, minmax: minmax(d[attr] for d in seq)

max_length = getExtremum(l_dict, 'length', max)
min_start = getExtremum(l_dict, 'start', min)
max_end = getExtremum(l_dict, 'end', max)
Jug
  • 540
  • 3
  • 7
  • Nice, but it scans the whole list for every item, O(3N). It would be interesting to time it against a single `for` loop. The OP says the list is huge – Pynchia Nov 08 '15 at 04:39