Python 从元组数组中提取所需索引
产生元组索引的最大值:(0,3)和(0,6)用于第一行[2,1,2,3,2,0,1,0];和(1,6)和(1,6)表示另一行[2,1,2,3,2,0,1,0] 下面打印所有结果,但我只想提取两行的第一个最大值,即使用元组[3,3]。所以,我需要的元组是(0,3)和(1,3) 如何从元组数组(即“maximas”)中提取它们Python 从元组数组中提取所需索引,python,arrays,numpy,Python,Arrays,Numpy,产生元组索引的最大值:(0,3)和(0,6)用于第一行[2,1,2,3,2,0,1,0];和(1,6)和(1,6)表示另一行[2,1,2,3,2,0,1,0] 下面打印所有结果,但我只想提取两行的第一个最大值,即使用元组[3,3]。所以,我需要的元组是(0,3)和(1,3) 如何从元组数组(即“maximas”)中提取它们 import numpy as np from scipy import signal y = np.array([[2, 1, 2, 3, 2, 0, 1, 0],
import numpy as np
from scipy import signal
y = np.array([[2, 1, 2, 3, 2, 0, 1, 0],
[2, 1, 2, 3, 2, 0, 1, 0]])
maximas = signal.argrelmax(y, axis=1)
print maximas
(array([0, 0, 1, 1], dtype=int64), array([3, 6, 3, 6], dtype=int64))
好的,纯Python方法将使用(行索引上的组)和列表理解:
>>> print y[kk]
[3 1 3 1]
好的,纯Python方法将使用(行索引上的组)和列表理解:
>>> print y[kk]
[3 1 3 1]
好的,纯Python方法将使用(行索引上的组)和列表理解:
>>> print y[kk]
[3 1 3 1]
好的,纯Python方法将使用(行索引上的组)和列表理解:
>>> print y[kk]
[3 1 3 1]
给定元组
maximas
,这里有一种可能的NumPy方法:
>>> from itertools import groupby
>>> from operator import itemgetter
>>> [max(g, key=lambda x: y[x])
for k, g in groupby(zip(*maximas), itemgetter(0))]
[(0, 3), (1, 3)]
这将signal.argrelmax
返回的坐标列表堆叠到数组a
中。np.unique的return\u index
参数用于查找每个行号的第一个索引。然后,我们可以使用这些第一个索引从a
检索相关行
这将返回一个数组,但您可以使用tolist()
将其转换为列表列表
要返回每行中最大值的第一列索引,只需从maximas[0]
获取np.unique
返回的索引,并使用它们对maximas[1]
进行索引。一行是这样的:
>>> a = np.column_stack(maximas)
>>> a[np.unique(a[:,0], return_index=True)[1]]
array([[0, 3],
[1, 3]], dtype=int64)
要从y
的每一行检索相应的值,可以使用np。选择:
>>> maximas[1][np.unique(maximas[0], return_index=True)[1]]
array([3, 3], dtype=int64)
给定元组maximas
,这里有一种可能的NumPy方法:
>>> from itertools import groupby
>>> from operator import itemgetter
>>> [max(g, key=lambda x: y[x])
for k, g in groupby(zip(*maximas), itemgetter(0))]
[(0, 3), (1, 3)]
这将signal.argrelmax
返回的坐标列表堆叠到数组a
中。np.unique的return\u index
参数用于查找每个行号的第一个索引。然后,我们可以使用这些第一个索引从a
检索相关行
这将返回一个数组,但您可以使用tolist()
将其转换为列表列表
要返回每行中最大值的第一列索引,只需从maximas[0]
获取np.unique
返回的索引,并使用它们对maximas[1]
进行索引。一行是这样的:
>>> a = np.column_stack(maximas)
>>> a[np.unique(a[:,0], return_index=True)[1]]
array([[0, 3],
[1, 3]], dtype=int64)
要从y
的每一行检索相应的值,可以使用np。选择:
>>> maximas[1][np.unique(maximas[0], return_index=True)[1]]
array([3, 3], dtype=int64)
给定元组maximas
,这里有一种可能的NumPy方法:
>>> from itertools import groupby
>>> from operator import itemgetter
>>> [max(g, key=lambda x: y[x])
for k, g in groupby(zip(*maximas), itemgetter(0))]
[(0, 3), (1, 3)]
这将signal.argrelmax
返回的坐标列表堆叠到数组a
中。np.unique的return\u index
参数用于查找每个行号的第一个索引。然后,我们可以使用这些第一个索引从a
检索相关行
这将返回一个数组,但您可以使用tolist()
将其转换为列表列表
要返回每行中最大值的第一列索引,只需从maximas[0]
获取np.unique
返回的索引,并使用它们对maximas[1]
进行索引。一行是这样的:
>>> a = np.column_stack(maximas)
>>> a[np.unique(a[:,0], return_index=True)[1]]
array([[0, 3],
[1, 3]], dtype=int64)
要从y
的每一行检索相应的值,可以使用np。选择:
>>> maximas[1][np.unique(maximas[0], return_index=True)[1]]
array([3, 3], dtype=int64)
给定元组maximas
,这里有一种可能的NumPy方法:
>>> from itertools import groupby
>>> from operator import itemgetter
>>> [max(g, key=lambda x: y[x])
for k, g in groupby(zip(*maximas), itemgetter(0))]
[(0, 3), (1, 3)]
这将signal.argrelmax
返回的坐标列表堆叠到数组a
中。np.unique的return\u index
参数用于查找每个行号的第一个索引。然后,我们可以使用这些第一个索引从a
检索相关行
这将返回一个数组,但您可以使用tolist()
将其转换为列表列表
要返回每行中最大值的第一列索引,只需从maximas[0]
获取np.unique
返回的索引,并使用它们对maximas[1]
进行索引。一行是这样的:
>>> a = np.column_stack(maximas)
>>> a[np.unique(a[:,0], return_index=True)[1]]
array([[0, 3],
[1, 3]], dtype=int64)
要从y
的每一行检索相应的值,可以使用np。选择:
>>> maximas[1][np.unique(maximas[0], return_index=True)[1]]
array([3, 3], dtype=int64)
您对纯Python解决方案开放吗?y[maximas]
产生[3 1 3 1]
。但是你想要的是y[(0,1),(3,3)]
产生[3,3]
,对吗?@hpaulj是的,这正是我想要的。你愿意接受纯Python解决方案吗?y[maximas]
产生[3,3 1]
。但是你想要的是y[(0,1),(3,3)]
产生[3,3]
,对吗?@hpaulj是的,这正是我想要的。你愿意接受纯Python解决方案吗?y[maximas]
产生[3,3 1]
。但是你想要的是y[(0,1),(3,3)]
产生[3,3]
,对吗?@hpaulj是的,这正是我想要的。你愿意接受纯Python解决方案吗?y[maximas]
产生[3,3 1]
。但是你想要的是y[(0,1),(3,3)]
产生[3,3]
,对吗?@hpaulj是的,这正是我想要的。我想返回[3,3]作为我的答案。我想返回[3,3]作为我的答案。我想返回[3,3]作为我的答案。我想返回[3,3]作为我的答案。