如何从两个元组列表创建numpy数组,但仅当元组是sam时

2024-09-30 22:12:41 发布

您现在位置:Python中文网/ 问答频道 /正文

为了进行图像分析,我用scipyimread加载了一个浮动图像。你知道吗

接下来,我让scipys argrelmax搜索轴0和1中的局部极大值,并将结果存储为元组数组。你知道吗

data = msc.imread(prediction1, 'F')
datarelmax_0 = almax(data, axis = 0)
datarelmax_1 = almax(data, axis = 1)

如何从两个列表创建一个numpy数组,其中只包含两个列表中的元组? 编辑: argrelmax创建一个包含两个数组的元组:

datarelmax_0 = ([1,2,3,4,5],[6,7,8,9,10])
datarelmax_1 = ([11,2,13,14,5], [11,7,13,14,10])

在中,您希望创建如下所示的numpy数组:

result_ar[(2,7),(5,10)]

Tags: 图像numpy列表data局部数组元组axis
1条回答
网友
1楼 · 发布于 2024-09-30 22:12:41

这种“天真”的方式怎么样?你知道吗

import numpy as np

result = np.array([x for x in datarelmax_0 if x in datarelmax_1])

很简单。也许有一个更好/更快/更奇特的方法,使用一些numpy方法,但现在应该可以了。你知道吗

编辑: 要回答已编辑的问题,可以执行以下操作:

result = [x for x in zip(datarelmax_0[0], datarelmax_0[1]) if x in zip(datarelmax_1[0], datarelmax_1[1])]

这给你

result = [(2, 7), (5, 10)]

如果使用

result = np.array(result)

看起来是这样的:

 result = array([[ 2,  7],
                 [ 5, 10]])

如果您对zip的功能感兴趣:

>>> zip(datarelmax_0[0], datarelmax_0[1])
[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]

>>> zip(datarelmax_1[0], datarelmax_1[1])
[(11, 11), (2, 7), (13, 13), (14, 14), (5, 10)]

相关问题 更多 >