Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:在元组列表中查找最小值和最大值_Python_List_Graphics - Fatal编程技术网

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

Python:在元组列表中查找最小值和最大值,python,list,graphics,Python,List,Graphics,我需要得到元组中每个位置的最小最大值 例如: alist的示例输出为 alist = [(1,3),(2,5),(2,4),(7,5)] 有什么简单的方法吗 min_x = 1 max_x = 7 min_y = 3 max_y = 5 这将首先解压列表,然后查找每个元组位置的最大值 map(max, zip(*alist)) 这也适用于列表中任何长度的元组。通用方法如下: >>> alist = [(1,3),(2,5),(2,4),(7,5)] >>&g

我需要得到元组中每个位置的最小最大值

例如: alist的示例输出为

alist = [(1,3),(2,5),(2,4),(7,5)]
有什么简单的方法吗

min_x = 1
max_x = 7

min_y = 3
max_y = 5
这将首先解压列表,然后查找每个元组位置的最大值

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]
>>> 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)
对于Python 3,您需要将创建
temp
的行更改为:

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]
这个想法可以抽象为一个在Python 2和Python 3中都能工作的函数:

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

无需使用
zip
,因此这简化为
map(max,*data)
(其中
data
是元组或相同长度列表的迭代器)。

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

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
对于python 3:

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

由于zip/map返回迭代器
,因此需要使用
list()

对列表进行多少次迭代?(4次、2次或1次)你能不能再多说一点在我面前的*是做什么的?如果没有*there…@Andrew Sure,
*
将列表拼接到函数的参数中,zip的行为就完全不同了。理解这一点的最佳方法是通过示例:
f(*[1,2,3])
f(1,2,3)
完全相同,因此
zip(*[(1,3)、(2,5)、(2,4)、(7,5)]
zip((1,3)、(2,5)、(2,4)、(7,5))相同,后者返回
[(1,2,7)、(3,5,4)、(5)]
。要获得完整的血淋淋的详细信息,请参阅中的谈论血淋淋的详细信息,请参阅sure to see tiwo's(确实应该在此处作为评论发布)关于不需要
zip
调用,只使用
map(max,*alist)
map(min,*alist)
。不确定您的意思,因为
map(max,*alist)
[7,6]
(就像
map(max,*iter(alist))
。@martineau
map(max,*alist)
返回
[7,5]
,就像
map(max,zip(*alist))
一样。@flornake:实际上这两个结果都是
[7,6]
在Python 2.7中。我最初的评论是因为原始问题中没有关于使用
zip
,所以我不理解这个“答案”的相关性。可能是关于@cobbal的答案的评论。它不适用于我使用
map(排序,zip(*alist))
@martineau它实际上是
[7,5]
。在这个问题的上下文中,这是最好的答案。@Graph:使用Python 2.7.12,我刚刚运行了
alist=[(1,6),(2,5),(2,4),(7,5)]
然后是
map(max,*alist)
(和
map(max,*iter(alist))
,结果是
[7,6]
。这是一个糟糕的解决方案,因为排序是
O(n×log(n))
虽然找到最小值是
O(n)
@greig:套用一个流行的美国成语,一般性并不是免费的。@tiwo改进的科巴尔答案不那么一般,但更简单:
列表(map(max,*[(1,9)、(2,8)、(3,7)])
返回
[3,9]
以及
min
-版本将返回
[1,7]
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)