大佬教程收集整理的这篇文章主要介绍了Python列表到Cython,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您需要将列表的内容显式复制到数组。例如…
cimport cython
from libc.stdlib cimport malloc, free
...
def process(a, int len):
cdef int *my_ints
my_ints = <int *>malloc(len(a)*cython.sizeof(int))
if my_ints is NulL:
raise MemoryError()
for i in xrange(len(a)):
my_ints[i] = a[i]
with nogil:
#Once you convert all of your Python types to C types, then you can release the GIL and do the real work
...
free(my_ints)
#convert BACk to python return type
return value
我想知道如何使用Cython将普通的python列表转换为C列表,对其进行处理并返回python列表。喜欢:
Python脚本:
import mymodule
a = [1,2,3,4,5,6]
len = len(a)
print(mymodule.process(a,len))
Cython脚本(mymodule.pyd):
cpdef process(a,int len):
cdef float y
for i in range(len):
y = a[i]
a[i] = y * 2
return a
我读了有关MemoryView和其他许多内容的文章,但我并没有真正理解发生的事情,很多示例都使用了Numpy(我不想用它来避免用户使用我的脚本下载一个大包……无论如何,我认为这是不正确的。不能与我的软件一起使用)。我需要一个非常简单的示例来了解正在发生的事情。
以上是大佬教程为你收集整理的Python列表到Cython全部内容,希望文章能够帮你解决Python列表到Cython所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。