tags:

views:

55

answers:

3

Hi, I've got a python list like

a = [
    [[1,2,(3,4)],[1,2,(5)],[-3,3,(3)],[8,-2,(5)]],
    [[1,2,(3,4,5)],[-1,222,(3,4,5)],[99,2,(3)],[8,-2,(4,5)]]
]

The tuple in each list element is total useless, please ignore that but delete them

I want to get the max min value from each list element in each position

The exepected output structure is

li = [[xmin, ymin, xmax, ymax], [xmin, ymin, xmax, ymax] ]

In this case li is [[-3, -2, 8, 3], [-1, -2,99, 222]]

So is there any easy to do ?Thank

+1  A: 

If you have access to numpy, you can use numpy.amax. You can use the axis parameter to get the maximum of a certain column of the array.

Space_C0wb0y
Could we not including other libraries?
+1  A: 

You can use zip:

a = [ [[1,2,(3,4)],[1,2,(5)],[-3,3,(3)],[8,-2,(5)] ],
[[1,2,(3,4,5)],[-1,222,(3,4,5)],[99,2,(3)],[8,-2,(4,5)] ] ]

>>> i, h = zip(*a[0]), zip(*a[1])
>>> [min(i[0]), min(i[1]), max(i[0]), max(i[1])]
[-3, -2, 8, 3]
>>> [min(h[0]), min(h[1]), max(h[0]), max(h[1])]
[-1, -2, 99, 222]
rubik
Could we use loop? since I may got more elements in a list than that?
But if the list structure is the same you can use the same method.
rubik
Use a list comprehension for larger inputs: `[[min(i[0]), min(i[1]), max(i[0]), max(i[1])] for i in (zip(*i) for i in a)]`.
Marcelo Cantos
A: 

First, a minor stylistic point, you should use tuples for small fix-length lists. I.e.: use (xmin, ymin, xmax, ymax) instead of [xmin, ymin, xmax, ymax].

[(min(t[0] for t in lst),
  min(t[1] for t in lst),
  max(t[0] for t in lst),
  max(t[1] for t in lst))
 for lst in a]
Marcelo Cantos