我想知道如何检查数组中是否存在一个值或一个对象,就像在 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/