Python:在元组列表中查找最小值,最大值 [英] Python: Find the min, max value in a list of tuples

查看:163
本文介绍了Python:在元组列表中查找最小值,最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  alist = [(1,3),(2,5),(2,4),(7,5)] 



我需要获取元组中每个位置的最小值。



Fox示例:
alist的输出结果是

  min_x = 1 
max_x = 7

min_y = 3
max_y = 5

有没有简单的方法可以做? / p>

解决方案

  map(max,zip(* alist))

这首先解压缩您的列表,然后找到每个元组位置的最大值

 >>> alist = [(1,3),(2,5),(2,4),(7,5)] 
>>> zip(* alist)
[(1,2,2,7),(3,5,4,5)]
>>> map(max,zip(* alist))
[7,5]
>>> map(min,zip(* alist))
[1,3]

也适用于列表中任意长度的元组。

alist = [(1,3),(2,5),(2,4),(7,5)]

I need to get the min max value for each position in tuple.

Fox example: The exepected output of alist is

min_x = 1
max_x = 7

min_y = 3
max_y = 5

Is there any easy way to do?

解决方案

map(max, zip(*alist))

This first unzips your list, then finds the max for each tuple position

>>> alist = [(1,3),(2,5),(2,4),(7,5)]
>>> zip(*alist)
[(1, 2, 2, 7), (3, 5, 4, 5)]
>>> map(max, zip(*alist))
[7, 5]
>>> map(min, zip(*alist))
[1, 3]

This will also work for tuples of any length in a list.

这篇关于Python:在元组列表中查找最小值,最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆