纯Python模式下的Cython功能返回字符串列表

发布于 2025-01-29 15:24:35 字数 3046 浏览 3 评论 0原文

我正在尝试将以下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2025-02-05 15:24:35

我不是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). Is append 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文