gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:24:29 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com