Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Min/Max From Nested List
#1
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]))
Reply
#2
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
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.
Reply
#5
'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.
Reply
#6
(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.
Reply
#7
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.
Reply
#8
For what I am doing both give me the results needed. Both solutions get a thumbs up.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,789 Jan-23-2022, 07:20 PM
Last Post: menator01
  Looping through nested elements and updating the original list Alex_James 3 2,070 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  shuffle a nested list giorgosmarga 11 11,787 Nov-12-2020, 07:04 PM
Last Post: perfringo
Question Save list with nested list into CSV SpongeB0B 1 5,289 Oct-12-2020, 07:26 AM
Last Post: bowlofred
  Struggling with nested list gr3yali3n 3 2,259 Jul-09-2020, 05:30 PM
Last Post: DPaul
  Nested Dictionary/List tonybrown3 5 3,074 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Help removing asterisk item in a nested list. bmcguire 3 2,551 Apr-06-2020, 02:35 PM
Last Post: snippsat
  Make nested system directories based on an unsorted list? koebi 0 1,573 Mar-25-2020, 01:14 PM
Last Post: koebi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020