如果numpy.delete()无法正常工作,则为简单用例

时间:2018-09-27 13:39:34

标签: python numpy

下面是一些代码:

24
[32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55]
20
[46, 35, 37, 54, 40, 49, 34, 48, 50, 38, 42, 47, 33, 52, 41, 36, 39, 44, 55, 
51]
24
[32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55]

它回馈:

require_once 'vendor/autoload.php'; //-- loading the google api client

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account/key/project-sfk28as8ff.json');

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$httpClient = $client->authorize();


// Your Firebase project ID
$project = "push-test-5f923";
// Creates a notification for subscribers to the debug topic
$message = [
    "message" => [
        "token" => "cO5hrNMFKQI:APA91bFm.......6IYy1phlxIJx2ZNA1",
        "notification" => [
            "body" => "This is an FCM notification message!",
            "title" => "FCM Message",
        ]
    ]
];
// Send the Push Notification - use $response to inspect success or errors
$response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);

var_dump($response);

如您所见,b的所有元素都出现在a中,但未被删除。不知道为什么。有任何想法吗?谢谢。

1 个答案:

答案 0 :(得分:1)

numpy.delete不会删除b中包含的元素,而是会删除a[b],换句话说,b需要包含要删除的索引。由于您的b仅包含大于a长度的值,因此不会删除任何值。当前,超出范围的索引将被忽略,但将来不会如此:

/usr/local/bin/ipython3:1: DeprecationWarning: in the future out of bounds indices will raise an error instead of being ignored by `numpy.delete`.
  #!/usr/bin/python3

一个纯Python解决方案是使用set

set_b = set(b)
c = np.array([x for x in a if x not in set_b])
# array([32, 43, 45, 51, 53])

并使用numpy广播创建掩码以确定要删除的值:

c = a[~(a[None,:] == b[:, None]).any(axis=0)]
# array([32, 43, 45, 51, 53])

它们与给定示例的速度大致相同,但采用numpy方法并占用更多内存(因为它生成包含ab的所有组合的2D矩阵)