开发者

Python: how to get the edge values from this list

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, pl开发者_Go百科ease 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


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.


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]


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜