minimum and maximum of nested lists

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 12250 Views - Last Post: 18 February 2016 - 06:39 PM Rate Topic: -----

#1 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,778
  • Joined: 27-December 13

minimum and maximum of nested lists

Posted 17 February 2016 - 03:27 PM

What is the best way to get minimum and maximum of nested list. I have now:
max([max(a[j])for j in range(len(a))])
and similar for minimum. 'a' is a nested list (two levels).
Can it be made better/simpler?
I imagine a library function is available, but havn't found it...
Is This A Good Question/Topic? 0
  • +

Replies To: minimum and maximum of nested lists

#2 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,842
  • Joined: 19-March 11

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:34 PM

Well, you want to get rid of the range part of that:

max([max (sublist) for sublist in outerlist])


but otherwise that looks pretty good to me.

EDIT: Of course, you'd really want to generalize it to allow for arbitrary levels of nesting. I don't know if you can write that as a comprehension, or if you'd want to, but can you write the function that does that? :)
Was This Post Helpful? 0
  • +
  • -

#3 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,778
  • Joined: 27-December 13

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:34 PM

ok, found an improvement:
min([min(j) for j in a])

Any better...?

EDIT: Post crossing jon's input, - thanks anyway

This post has been edited by DK3250: 17 February 2016 - 03:35 PM

Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,842
  • Joined: 19-March 11

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:37 PM

Better, but look at your names and mine. Which one is easier to understand?
Was This Post Helpful? 1
  • +
  • -

#5 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,260
  • Joined: 12-December 12

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:39 PM

What's wrong with doing it in two steps..?

nest = [ [ 1, 3, 4], [6, 7, 7, 4] ]

maxx, minx = None, None
for x, y in zip(nest, nest):
    if maxx == None:
        maxx, minx = max(x), min(y)
    else:
        maxx, minx = max(maxx, max(x)), min(minx, min(y))

print(maxx, minx) # 7, 1

Edit, actually, the zip is unnecessary:
nest = [ [ 1, 3, 4], [6, 7, 7, 4] ]

maxx, minx = None, None
for x in nest:
    if maxx == None:
        maxx, minx = max(x), min(x)
    else:
        maxx, minx = max(maxx, max(x)), min(minx, min(x))

print(maxx, minx) # 7, 1

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,842
  • Joined: 19-March 11

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:43 PM

The zip will also produce errors, since it acts procrusteanishly. (truncates the longer list to the length of the shortest)
Was This Post Helpful? 0
  • +
  • -

#7 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,778
  • Joined: 27-December 13

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:43 PM

You are right, of cause!
The 'j' is a remnant from the counter using range().
The 'a' is actually my file-name, short for Array.
Not a good name, but I'm building a 2D noise function and need to write the Array-name many times - so laziness took me. But you are right; names should always be informative.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,260
  • Joined: 12-December 12

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:44 PM

Or
maxx2, minx2 = max([max(x) for x in nest]), min([min(y) for y in nest])
print(maxx2, minx2)

Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,842
  • Joined: 19-March 11

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:45 PM

This also works:
max ([sorted(sublist)[-1] for sublist in outerlist])

Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,260
  • Joined: 12-December 12

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:46 PM

View Postjon.kiparsky, on 17 February 2016 - 10:43 PM, said:

The zip will also produce errors, since it acts procrusteanishly. (truncates the longer list to the length of the shortest)

but they're the same list/length ;) (not crab-like)
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,842
  • Joined: 19-March 11

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:47 PM

Are they?

Quote

nest = [ [ 1, 3, 4], [6, 7, 7, 4] ]



More important, do you want to limit yourself to lists of equal length?
Was This Post Helpful? 0
  • +
  • -

#12 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,778
  • Joined: 27-December 13

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 03:58 PM

andrewsw, I like your second code snip - think I can use it for other purposes.
For the task at hand, checking a 513 X 513 array, I imagine is will be slower than the one-liner.
(Sticking my neck out here - did not check up on execution speed).

CLARIFICATION: I'm referring to post #5

This post has been edited by DK3250: 17 February 2016 - 04:01 PM

Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,842
  • Joined: 19-March 11

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 04:02 PM

There's no real reason to assume that shorter == faster. However, for processing large arrays, you might find that numpy has something useful to offer you.
Was This Post Helpful? 0
  • +
  • -

#14 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,778
  • Joined: 27-December 13

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 04:06 PM

Last I checked (one year ago or so), I couldn't find NumPy for Python 3.x
I check again, as NumPy clearly is faster and have simpler notation of 2D array.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,842
  • Joined: 19-March 11

Re: minimum and maximum of nested lists

Posted 17 February 2016 - 04:15 PM

Looking at the docs, I see:

Quote

Prerequisites

Building NumPy requires the following software installed:

Python 2.6.x, 2.7.x, 3.2.x or newer

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2