python - 如何根据两列的最大值拆分数组

标签 python arrays numpy

我想根据两列的值拆分一个 numpy 数组。我想在前两列(同时)达到最大值后在索引处拆分。每列数次达到最大值。可以单独看到每一列的最大值(当另一列不在最大值时),但是当它们都处于最大值时我需要分开。假设我有

arr =  [[ 1., 5, 12],
        [ 1., 9,  5],
        [15., 5,  5],
        [25., 7,  4],
        [25., 9,  4],
        [1.5, 4, 10],
        [ 1., 8,  7],
        [20., 5,  6],
        [25., 8,  3],
        [25., 9,  3]]

我想得到:

arr_1 = [[ 1., 5, 12],
         [ 1., 9,  5],
         [15., 5,  5],
         [25., 7,  4],
         [25., 9,  4]]

arr_2 = [[1.5, 4, 10],
         [ 1., 8,  7],
         [20., 5,  6],
         [25., 8,  3],
         [25., 9,  3]]

最佳答案

假设您希望输出为列表的列表,您可以遍历原始数组的元素并查找“分隔”元素。

一种可能的实现方式:

def split_at_max(arr):
    m0 = max(a[0] for a in arr)
    m1 = max(a[1] for a in arr)
    res = [[]]
    for i,a in enumerate(arr):
        res[-1].append(a)
        if (a[:2] == [m0, m1]) and (i != len(arr) - 1):
            res.append([])
   return res

关于python - 如何根据两列的最大值拆分数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64063499/

相关文章:

python - 我如何使用 matplotlib 使用非数值为绘图着色?

python - 如何使用 Pandas 计算逗号分隔列的平均值?

php - 数组内爆 '<' ,奇怪的行为

arrays - Ruby:根据日期分组时计算数组数组的平均值

python - 使用 .loc 的多列分配 - Pandas

python - gensim TransformedCorpus 数据到数组的高效转换

python - 调用时出现 digitalocean 脚本错误

python - 在 python 字典中查找键并返回其父项

php - 我可以在类变量中添加没有赋值的 PHP 数组键吗?

python - 存储和读取大量 3d 数据集的节省空间的方法?