Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.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查找索引1的最大值&;列表中的2个_Python - Fatal编程技术网

Python查找索引1的最大值&;列表中的2个

Python查找索引1的最大值&;列表中的2个,python,Python,列表中 [ [[ 979, 2136, 3427, 2221]], [[1497, 697, 2843, 721]], [[2778, 2194, 3903, 2233]], ] 这样迭代 for line in lines: for x1, y1, x2, y2 in line: 我想找出(x1或x2,以最大值为准)和(y1 y2,以最大值为准)您可以这样做: [*map(lambda x: list(map(max, [x[0][:2], x[0][2:]])), m

列表中

[
[[ 979, 2136, 3427, 2221]],

 [[1497,  697, 2843,  721]],

 [[2778, 2194, 3903, 2233]],
]
这样迭代

for line in lines:
    for x1, y1, x2, y2 in line:
我想找出(x1或x2,以最大值为准)和(y1 y2,以最大值为准)

您可以这样做:

[*map(lambda x: list(map(max, [x[0][:2], x[0][2:]])), mylist)]
如果您的点只有正值,您可以将max_x和max_y初始化为0。

我更喜欢更详细(但我认为也更可读)的Nicolas答案版本,该版本保留
for
循环,并使用命名元组进行更有意义的输出:

from collections import namedtuple

Coordinate = namedtuple('Coordinate', ['x', 'y'])

lines = [[[ 979, 2136, 3427, 2221]], [[1497,  697, 2843,  721]], [[2778, 2194, 3903, 2233]]]

maximums = []

for line in lines:
    for x1, y1, x2, y2 in line:
        maximums.append(Coordinate(x=max(x1, x2), y=max(y1, y2)))

print(maximums)
# prints [Coordinate(x=3427, y=2221), Coordinate(x=2843, y=721), Coordinate(x=3903, y=2233)]
print([Coordinate(x=3427, y=2221), Coordinate(x=2843, y=721), Coordinate(x=3903, y=2233)])
# prints [(3427, 2221), (2843, 721), (3903, 2233)]

以下是如何找到每组的最大值:

ls1 = [[[ 979, 2136, 3427, 2221]],
       [[1497,  697, 2843,  721]],
       [[2778, 2194, 3903, 2233]]]

ls2 = [[max(a[0][:2]),
        max(a[0][2:])]
       for a in ls1]

print(ls2)
输出:

[[2136, 3427],
 [1497, 2843],
 [2778, 3903]]
2778 3903


要查找所有
x
s和
y
s的最大值:

ls1 = [[[ 979, 2136, 3427, 2221]],
       [[1497,  697, 2843,  721]],
       [[2778, 2194, 3903, 2233]]]

x = max([i for j in [a[0][:2] for a in ls1] for i in j])
y = max([i for j in [a[0][2:] for a in ls1] for i in j])

print(x, y)
输出:

[[2136, 3427],
 [1497, 2843],
 [2778, 3903]]
2778 3903

输出是什么?您能否正确设置输入的格式?
max(x1,x2)
max(y1,y2)
?您是要查找每行的最大值(小列表)还是总的最大值?您确定要使用列表列表吗?这看起来像是一个
numpy.ndarray
的输出…我喜欢你的答案,但是寻找所有元素的绝对最大值,所有x和所有y,你能包括你的预期输出吗?恐怕我不明白你想要什么,给定的例子,x是3903,y是2233