纯Python模式下的Cython功能返回字符串列表
我正在尝试将以下Python函数转换
def python_compare(a: str, b: str) -> list:
n = len(a)
result = []
for i in range(n):
letter1 = a[i]
letter2 = b[i]
if letter1 != letter2:
mut = f'{letter1}{i}{letter2}'
result.append(mut)
return result
python_compare('aaa', 'aab')
output: ['a2b']
为以纯Python模式编写的Cython函数。
我正在jupyter笔记本中运行代码,因此%% Cython
行。这就是我尝试的:
%%cython -a
import cython
@cython.cfunc
def compare(a: cython.basestring, b: cython.basestring) -> cython.array:
n = cython.int = len(a)
i: cython.int
letter1: cython.basestring
letter2: cython.basestring
mut: cython.basestring
result: cython.array(cython.basestring, n)
for i in range(n):
letter1 = a[i]
letter2 = b[i]
if letter1 != letter2:
mut = f'{letter1}{i}{letter2}'
result.append(mut)
return result
但是,这给了我输出:
Error compiling Cython file:
------------------------------------------------------------
...
import cython
@cython.cfunc
def compare(a: cython.basestring, b: cython.basestring) -> cython.array:
^
------------------------------------------------------------
8a1.pyx:5:65: Not a type
Error compiling Cython file:
------------------------------------------------------------
...
import cython
@cython.cfunc
def compare(a: cython.basestring, b: cython.basestring) -> cython.array:
^
------------------------------------------------------------
8a1.pyx:5:21: 'basestring' not a valid cython attribute or is being used incorrectly
Error compiling Cython file:
------------------------------------------------------------
...
import cython
@cython.cfunc
def compare(a: cython.basestring, b: cython.basestring) -> cython.array:
^
------------------------------------------------------------
8a1.pyx:5:43: 'basestring' not a valid cython attribute or is being used incorrectly
Error compiling Cython file:
------------------------------------------------------------
...
letter1 = a[i]
letter2 = b[i]
if letter1 != letter2:
mut = f'{letter1}{i}{letter2}'
result.append(mut)
^
------------------------------------------------------------
8a1.pyx:20:12: local variable 'result' referenced before assignment
Error compiling Cython file:
------------------------------------------------------------
...
letter2 = b[i]
if letter1 != letter2:
mut = f'{letter1}{i}{letter2}'
result.append(mut)
return result
^
------------------------------------------------------------
8a1.pyx:21:11: local variable 'result' referenced before assignment
有人可以帮助我在Cythons Pure Python模式中重写该功能,以使其编译和工作吗?
所有的建议和评论都非常感谢! 谢谢,
威廉
I am trying to convert the following python function:
def python_compare(a: str, b: str) -> list:
n = len(a)
result = []
for i in range(n):
letter1 = a[i]
letter2 = b[i]
if letter1 != letter2:
mut = f'{letter1}{i}{letter2}'
result.append(mut)
return result
python_compare('aaa', 'aab')
output: ['a2b']
To a cython function written in pure python mode.
I am running the code in a jupyter notebook, hence the %%cython
line. This is what I am trying:
%%cython -a
import cython
@cython.cfunc
def compare(a: cython.basestring, b: cython.basestring) -> cython.array:
n = cython.int = len(a)
i: cython.int
letter1: cython.basestring
letter2: cython.basestring
mut: cython.basestring
result: cython.array(cython.basestring, n)
for i in range(n):
letter1 = a[i]
letter2 = b[i]
if letter1 != letter2:
mut = f'{letter1}{i}{letter2}'
result.append(mut)
return result
However, this gives me the output:
Error compiling Cython file:
------------------------------------------------------------
...
import cython
@cython.cfunc
def compare(a: cython.basestring, b: cython.basestring) -> cython.array:
^
------------------------------------------------------------
8a1.pyx:5:65: Not a type
Error compiling Cython file:
------------------------------------------------------------
...
import cython
@cython.cfunc
def compare(a: cython.basestring, b: cython.basestring) -> cython.array:
^
------------------------------------------------------------
8a1.pyx:5:21: 'basestring' not a valid cython attribute or is being used incorrectly
Error compiling Cython file:
------------------------------------------------------------
...
import cython
@cython.cfunc
def compare(a: cython.basestring, b: cython.basestring) -> cython.array:
^
------------------------------------------------------------
8a1.pyx:5:43: 'basestring' not a valid cython attribute or is being used incorrectly
Error compiling Cython file:
------------------------------------------------------------
...
letter1 = a[i]
letter2 = b[i]
if letter1 != letter2:
mut = f'{letter1}{i}{letter2}'
result.append(mut)
^
------------------------------------------------------------
8a1.pyx:20:12: local variable 'result' referenced before assignment
Error compiling Cython file:
------------------------------------------------------------
...
letter2 = b[i]
if letter1 != letter2:
mut = f'{letter1}{i}{letter2}'
result.append(mut)
return result
^
------------------------------------------------------------
8a1.pyx:21:11: local variable 'result' referenced before assignment
Can someone help me to rewrite the function in cythons pure python mode so that it compiles and works?
All advices and comments are much appreciated!
Thanks,
William
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是Cython专家,但似乎您在尝试附加某些内容之前没有创建
result
数组(错误消息是一个强烈的提示:“引用的本地变量'结果'分配之前”)。看来您仅声明其类型(
结果:Cython.Array(Cython.basestring,n)
),然后在尝试附加之前不会为此变量分配任何内容。顺便说一句,是否真的有可能将附加到数组?您声明某些大小(
n
)的内容。附加
应该增加此数组的大小吗?如果不是,则在阵列中应该插入数据?在您的普通Python代码中,您从一个空列表开始。然后,您将事情附加到最后。这看起来还不错,但可能不会使用数组在Cython中翻译。
我对非周期专家的直觉是,您不应将其附加到数组中,而是应该将价值定为就地(C风格),为此,您需要索引。您的循环变量可以
i
扮演这个角色吗?I'm not a Cython expert, but it seems that you do not create your
result
array prior to trying to append something to it (The error message is a strong hint: "local variable 'result' referenced before assignment").It seems that you only declare its type (
result: cython.array(cython.basestring, n)
), and then do not assign anything to this variable before trying to append.By the way, is it really possible to
append
to an array? You declare something of a certain size (n
). Isappend
supposed to increase the size of this array? If not, where in the array the data is supposed to be inserted?In your plain Python code, you start with an empty list. Then you append things to the end. This looks OK, but may not be translated literally in Cython using an array.
My intuition of non-Cython expert is that instead of appending to the array, you should modify values in-place (C-style), and for that, you need an index. Could your loop variable
i
play this role?