检查一个值是否存在于 Cython 的数组中

标签 c arrays cython

我想知道如何检查数组中是否存在一个值或一个对象,就像在 python 中一样:

a = [1,2,3,4,5]
b = 4
if b in a:
    print("True!")
else:
    print("False")

我想知道 cython 中是否已经存在类似的东西。我有一个指针结构对象数组;我想知道对象是否存在于这个数组中。

喜欢

cdef Node *array

array = <Node *>malloc( 5 * cython.sizeof(Node))

for i in range(5):
     array[i].index = i

cdef Node test = array[3]

if test in array:
    print("True!")

cdef struct Node:
    int index

上面的代码不正确,但它说明了我的意思。

最佳答案

您几乎必须遍历数组并检查每个元素。

#include <stdbool.h>

bool isvalueinarray(int val, int *arr, int size){
    int i;
    for (i=0; i < size; i++) {
        if (arr[i] == val)
            return true;
    }
    return false;
}

关于检查一个值是否存在于 Cython 的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15094834/

相关文章:

c - 无法将 header 设置为指向链表中的新节点

c - 我想在 OpenCV 的不同窗口中显示输出图像

php - 删除每第 N 个字母(循环)

python - 将 3D numpy 矩阵 reshape 为 2D numpy 矩阵,保持行位置

python - 将 cython cdef 扩展数组设置为零

macos - 如何在 Cython 中链接 Mac OS 框架

c - K&R 中的指针加法

c - 是否有一种传统方法可以在 C 中花费恰好一个时钟周期?

c++ - 我继续在函数中从另一个数组修改的数组中获取垃圾值

c - 如何使用 Cython 将 C 结构数组转换为 numpy 数组