Skip to main content
 首页 » 编程设计

python之F2Py:使用通过 Python 调用的 Fortran 中的可分配数组

2025年05月04日7傻小

使用F2Py编译适合在Python中使用的Fortran例程,下面这段代码成功编译配置gfortran为编译器在使用 F2Py 时,然而,在 Python 中调用时会引发运行时错误!
有什么意见和解决方案吗?

function select(x) result(y) 
   implicit none 
   integer,intent(in):: x(:)  
   integer:: i,j,temp(size(x)) 
   integer,allocatable:: y(:) 
   j = 0 
   do i=1,size(x) 
      if (x(i)/=0) then 
         j = j+1 
         temp(j) = x(i) 
      endif 
   enddo 
   allocate(y(j)) 
   y = temp(:j) 
end function select 

可以找到类似的 StackOverflow 帖子 here .

请您参考如下方法:

看看这篇文章http://www.shocksolution.com/2009/09/f2py-binding-fortran-python/ ,特别是在示例和含义上

!f2py depend(len_a) a, bar 

然而,作者并没有涉及生成不同大小的数组的问题。