首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >纯python模式下返回字符串列表的Cython函数。

纯python模式下返回字符串列表的Cython函数。
EN

Stack Overflow用户
提问于 2022-05-17 08:45:41
回答 1查看 166关注 0票数 0

我正在尝试转换以下python函数:

代码语言:javascript
复制
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行。这就是我想要做的:

代码语言:javascript
复制
%%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

但是,这给出了输出:

代码语言:javascript
复制
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纯python模式重写函数以便它编译和工作吗?

所有的建议和评论是非常感谢!谢谢,

威廉

EN

回答 1

Stack Overflow用户

发布于 2022-05-19 09:46:33

我不是Cython专家,但似乎在尝试向其添加某些内容之前没有创建result数组(错误消息是一个强烈的提示:“赋值前引用的局部变量‘结果’”)。似乎您只声明了它的类型(result: cython.array(cython.basestring, n)),然后在尝试追加之前不要为这个变量赋值任何东西。

顺便问一下,是否真的有可能对数组进行append?您可以声明一定大小的东西(n)。append应该增加这个数组的大小吗?如果没有,则应该在数组中插入数据的位置?

在普通Python代码中,以空列表开始。然后你把东西附加到最后。这看起来没问题,但可能无法用数组在Cython中逐字翻译。

我对非Cython专家的直觉是,您不应该附加到数组中,而应该修改相应的值(C风格),为此需要一个索引。你的循环变量i能扮演这个角色吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72270867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档