检查 Cython 中的数组中是否存在值 [英] Check if a value exists in an array in Cython
本文介绍了检查 Cython 中的数组中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何检查数组中是否存在值或对象,例如在 python 中:
I want to know how to check if a value or a object exists in an array, like in python:
a = [1,2,3,4,5]
b = 4
if b in a:
print("True!")
else:
print("False")
我想知道cython中是否已经存在类似的东西.我有一个指针结构对象数组;我想知道该对象是否存在于该数组中.
I want to know if something similar already exists in cython. I have a struct object array of pointers; I want to know if the object exists in this array.
喜欢
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
上面的代码不正确,但它说明了我的意思.
The code above is not correct, but it illustrates what I mean.
推荐答案
您几乎必须遍历数组并检查每个元素.
You pretty much have to iterate through the array and check each element.
#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 中的数组中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文