首页 > 解决方案 > 在 Cython 中返回字典的函数的声明

问题描述

在 Cython 中,我可以声明一个返回整数的函数:

cdef int fx(int myInt):
    return myInt

此处记录的所有数据类型相同。

一个返回整数数组的函数

cdef int[:] fx():
   ...
  return myIntArray

是否可以声明一个返回字典的函数?如何?

谢谢!

免责声明:我是 cython 的新手。请评论或编辑我的错误。

标签: dictionarycython

解决方案


至少对于从 python 调用函数,是dict

例如字典.pyx

# function can be called in cython
cdef dict print_dict():                                                         
    cdef dict a                                                                 
    a = {'a':'areli', 'b':'asdasd'}                                             
    return a                                                                    
                                                                                

# function to be called in python                                                                            
def test_dict():                                                                
    return print_dict()   

在蟒蛇中:

import Dict                                                                                                                                                                         
Dict.test_dict()                                                                                                                                                                

给出:

{'a': 'areli', 'b': 'asdasd'}

推荐阅读