当条件为True python时获取n维数组的索引

时间:2018-06-11 08:28:40

标签: python numpy slice indices

我有一个三维数组。我希望在条件满足时得到指数。这是一个例子:

{
"id": "9710ef23-aae5-4215-a45b-641185140722",
"title": "Free Plan",
"planId": "freeplan",
"ranking": 1,
"description": "Most basic features",
"startFrom": "2018-01-01",
"monthlyCost": 0,
"details": [
  // supressed
],
"users": [
    {
        "id": "11111111-2222-333-4444-5555555555555",
        "enrolledIn": "2017-07-30 08:00:01"
    },
    {
        "id": "4760c977-6bcf-497d-96eb-c43e5bcf1ab9",
        "enrolledIn": "1981-07-05 08:00:01"
    }
],

import numpy as np np.random.seed(123) x = np.array(np.random.rand(20)) x = x.reshape(2,5,2) # x looks like this array([[[ 0.69646919, 0.28613933], [ 0.22685145, 0.55131477], [ 0.71946897, 0.42310646], [ 0.9807642 , 0.68482974], [ 0.4809319 , 0.39211752]], [[ 0.34317802, 0.72904971], [ 0.43857224, 0.0596779 ], [ 0.39804426, 0.73799541], [ 0.18249173, 0.17545176], [ 0.53155137, 0.53182759]]]) # now I want to get the indices where the value is less than 0.5 idx = np.where(x<0.5) print idx (array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]), array([0, 1, 2, 4, 4, 0, 1, 1, 2, 3, 3]), array([1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1])) 返回元组。我如何提取指数?比如np.where

0 个答案:

没有答案