Python:在元组列表中查找最小值、最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4002796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:47:50  来源:igfitidea点击:

Python: Find the min, max value in a list of tuples

pythonlistgraphics

提问by user483144

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

Fox 示例:alist 的预期输出为

min_x = 1
max_x = 7

min_y = 3
max_y = 5

Is there any easy way to do?

有什么简单的方法吗?

回答by cobbal

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.

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

回答by John La Rooy

>>> from operator import itemgetter
>>> alist = [(1,3),(2,5),(2,4),(7,5)]
>>> min(alist)[0], max(alist)[0]
(1, 7)
>>> min(alist, key=itemgetter(1))[1], max(alist, key=itemgetter(1))[1]
(3, 5)

回答by martineau

A generalized approach would be something like this:

一个通用的方法是这样的:

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

temp = map(sorted, zip(*alist))
min_x, max_x, min_y, max_y = temp[0][0], temp[0][-1], temp[1][0], temp[1][-1]

For Python 3, you'd need change the line that createstempto:

对于 Python 3,您需要将创建的行更改temp为:

temp = tuple(map(sorted, zip(*alist)))

The idea can be abstracted into a function which works in both Python 2 and 3:

这个想法可以抽象为一个在 Python 2 和 3 中都可以工作的函数:

from __future__ import print_function
try:
    from functools import reduce  # moved into functools in release 2.6
except ImportError:
    pass

# readable version
def minmaxes(seq):
    pairs = tuple()
    for s in map(sorted, zip(*seq)):
        pairs += (s[0], s[-1])
    return pairs

# functional version
def minmaxes(seq):
    return reduce(tuple.__add__, ((s[0], s[-1]) for s in map(sorted, zip(*seq))))

alist = [(1,6), (2,5), (2,4), (7,5)]
min_x, max_x, min_y, max_y = minmaxes(alist)
print(' '.join(['{},{}']*2).format(*minmaxes(alist)))  # 1,7 4,6

triplets = [(1,6,6), (2,5,3), (2,4,9), (7,5,6)]
min_x, max_x, min_y, max_y, min_z, max_z = minmaxes(triplets)
print(' '.join(['{},{}']*3).format(*minmaxes(triplets)))  # 1,7 4,6 3,9

回答by tiwo

At least with Python 2.7, the "zip" is not necessary, so this simplifies to map(max, *data)(where datais an iterator over tuples or lists of the same length).

至少在Python 2.7 中,“zip”不是必需的,因此这简化为map(max, *data)( where datais an iterator over tuples or lists of the same length)。

回答by toppare

Another solution using enumerate and list comprehension

使用枚举和列表理解的另一种解决方案

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

for num, k in enumerate(['X', 'Y']):
    print 'max_%s' %k, max([i[num] for i in alist])
    print 'min_%s' %k, min([i[num] for i in alist])

回答by Leo103

For python 3:

对于蟒蛇 3:

alist = [(1,3),(2,5),(2,4),(7,5)]    
[x_range, y_range] = list(zip(map(min, *test_list), map(max, *alist)))

print(x_range, y_range) #prints: (1, 7) (3, 5)

Since zip/map returns an iterator <object at 0x00>you need to use list()

由于 zip/map 返回一个迭代器,<object at 0x00>您需要使用list()