Posts: 4
Threads: 1
Joined: Jan 2019
I have searched and found similar request but not able to nail it down. I have a nested list such as:
[[12, 16], [0, 18], [12, 20], [12, 24], [0, 28], [28, 32], [0, 36], [12, 40], [32, 44], [12, 48]] I am attempting to get the pair of numbers which contain the min value for the first element and the max for second. In the case above the correct return should be [0,36]. I have been testing with the below statement. This doesn't seem to give back what I expected, I know I maybe missing something. When I execute the statement, I get [0, 18], or the first instance of the min value. Other than writing several loops to get the desired result, is there another way? Thanks
min(alist, key=lambda item: (item[0], -item[1]))
Posts: 4,220
Threads: 97
Joined: Sep 2016
Jan-31-2019, 05:09 PM
(This post was last modified: Jan-31-2019, 05:17 PM by ichabod801.)
(Jan-31-2019, 05:03 PM)snowman24 Wrote: I am attempting to get the pair of numbers which contain the min value for the first element and the max for second. In the case above the correct return should be [0,36].
Given that 36 is not the max for the second value, I assume that you want to find the maximum second value among those with the minimum first value. Which is not exactly what you said. To do that, sort by the second value, with reverse = True. Then sort by the first value. The first item will be what you want.
>>> x = [[12, 16], [0, 18], [12, 20], [12, 24], [0, 28], [28, 32], [0, 36], [12, 40], [32, 44], [12, 48]]
>>> x.sort(key = lambda a: a[1], reverse = True)
>>> x
[[12, 48], [32, 44], [12, 40], [0, 36], [28, 32], [0, 28], [12, 24], [12, 20], [0, 18], [12, 16]]
>>> x.sort(key = lambda a: a[0])
>>> x
[[0, 36], [0, 28], [0, 18], [12, 48], [12, 40], [12, 24], [12, 20], [12, 16], [28, 32], [32, 44]] Edit: clarified my clarification, with code.
Posts: 1,950
Threads: 8
Joined: Jun 2018
(Jan-31-2019, 05:03 PM)snowman24 Wrote: [[12, 16], [0, 18], [12, 20], [12, 24], [0, 28], [28, 32], [0, 36], [12, 40], [32, 44], [12, 48]] I am attempting to get the pair of numbers which contain the min value for the first element and the max for second. In the case above the correct return should be [0,36].
Why [0, 36] is expected result? Why not [12, 48]?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 4
Threads: 1
Joined: Jan 2019
Jan-31-2019, 05:20 PM
(This post was last modified: Jan-31-2019, 05:40 PM by snowman24.)
I posted I need the min of the first element and max of the second, should have added that is in the same pair. In the list [0,36], 0 is min of the first element and 36 would be the max for second in the pairs. When my statement posted above runs it gets the 0 but doesn't get the max value paired with it, it returns 18.
(Jan-31-2019, 05:09 PM)ichabod801 Wrote: (Jan-31-2019, 05:03 PM)snowman24 Wrote: I am attempting to get the pair of numbers which contain the min value for the first element and the max for second. In the case above the correct return should be [0,36].
Given that 36 is not the max for the second value, I assume that you want to find the maximum second value among those with the minimum first value. Which is not exactly what you said. To do that, sort by the second value, with reverse = True. Then sort by the first value. The first item will be what you want.
>>> x = [[12, 16], [0, 18], [12, 20], [12, 24], [0, 28], [28, 32], [0, 36], [12, 40], [32, 44], [12, 48]]
>>> x.sort(key = lambda a: a[1], reverse = True)
>>> x
[[12, 48], [32, 44], [12, 40], [0, 36], [28, 32], [0, 28], [12, 24], [12, 20], [0, 18], [12, 16]]
>>> x.sort(key = lambda a: a[0])
>>> x
[[0, 36], [0, 28], [0, 18], [12, 48], [12, 40], [12, 24], [12, 20], [12, 16], [28, 32], [32, 44]] Edit: clarified my clarification, with code.
Yes, this is what I was hoping for. Thank you for the time.
Posts: 1,950
Threads: 8
Joined: Jun 2018
'out'sane comprehension using min and max to achieve desired result:
>>> a = [[12, 16], [0, 18], [12, 20], [12, 24], [0, 28], [28, 32], [0, 36], [12, 40], [32, 44], [12, 48]]
>>> max(el for el in sorted(a) if el[0] == min(sorted(a)[0]))
[0, 36]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 4
Threads: 1
Joined: Jan 2019
(Jan-31-2019, 05:45 PM)perfringo Wrote: 'out'sane comprehension using min and max to achieve desired result:
>>> a = [[12, 16], [0, 18], [12, 20], [12, 24], [0, 28], [28, 32], [0, 36], [12, 40], [32, 44], [12, 48]]
>>> max(el for el in sorted(a) if el[0] == min(sorted(a)[0]))
[0, 36]
Outstanding one liner! That's kinda what I was shooting for originally but wasn't there.
Posts: 1,950
Threads: 8
Joined: Jun 2018
Actually I think that ichabod801 approach is better. His code can be refactored to oneliner as well:
>>> a = [[12, 16], [0, 18], [12, 20], [12, 24], [0, 28], [28, 32], [0, 36], [12, 40], [32, 44], [12, 48]]
>>> sorted(a, key=lambda x : (x[0], -x[1]))[0]
[0, 36]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 4
Threads: 1
Joined: Jan 2019
For what I am doing both give me the results needed. Both solutions get a thumbs up.
|