是否有 Fortran 等价于 Python 的 for-else 语句?



是否有python的for-else语句的fortran?

例如,以下将数字列表列为不同范围。在Python中,是:

absth = [1, 2, 3, 4, 5]
vals = [.1, .2, .5, 1.2, 3.5, 3.7, 16.8, 19.8, 135.60]

counts = [0] * len(absth)
for v in vals:
    for i, a in enumerate(absth):
        if v < a:
            counts[i] += 1
            break
    else:
        counts[-1] += 1

在fortran中,这也是如此:

do iv = 1, nvals
  is_in_last_absth = .true.
  do ia = 1, nabsth - 1
    if vals(iv) < absth(ia) then
      counts(ia) = counts(ia) + 1
      is_in_last_absth = .false.
      exit
    end if
  end do
  if (is_in_last_absth) then
    counts(nabsth) = counts(nabsth) + 1
  end if
end do

但是,有没有办法不必使用is_in_last_absth并将其替换为Python中的else

没有直接等效的python构造。

请注意,可以通过检查循环后的DO变量值来检测具有计数循环控制的DO循环的早期终止。

do iv = 1, nvals
  do ia = 1, nabsth - 1
    if (vals(iv) < absth(ia)) then
      counts(ia) = counts(ia) + 1
      exit
    end if
  end do
  ! If the loop terminates because it completes the iteration 
  ! count (and not because the exit statement is executed) then 
  ! the do variable is one step beyond the value it had 
  ! during the last iteration.
  if (ia == nabsth) then
    counts(nabsth) = counts(nabsth) + 1
  end if
end do

退出声明也可以跳出不仅仅是循环:

do iv = 1, nvals
  outer_block: block
    do ia = 1, nabsth - 1
      if (vals(iv) < absth(ia)) then
        counts(ia) = counts(ia) + 1
        exit outer_block
      end if
    end do
    counts(nabsth) = counts(nabsth) + 1
  end block outer_block
end do

和一个周期语句可以循环任何确实构造的语句嵌套在:

outer_loop: do iv = 1, nvals
  do ia = 1, nabsth - 1
    if (vals(iv) < absth(ia)) then
      counts(ia) = counts(ia) + 1
      cycle outer_loop
    end if
  end do
  counts(nabsth) = counts(nabsth) + 1
end do outer_loop

如果问题是专门说明一系列数字的问题,而 absth是每个垃圾箱上的上限(bar bar last没有上限),那么我可能会写一些东西这样:

PROGRAM test
  IMPLICIT NONE
  INTEGER :: ix   
  INTEGER, DIMENSION(5) :: absth = [1, 2, 3, 4, 5]   
  REAL, DIMENSION(9) :: vals = [.1, .2, .5, 1.2, 3.5, 3.7, 16.8, 19.8, 135.60]   
  INTEGER, DIMENSION(SIZE(absth)+1) :: bins
  bins = 0
  DO ix = 1, SIZE(bins)-1
     bins(ix) = COUNT(vals<absth(ix))   
  END DO   
  bins(ix) = COUNT(vals)
  bins = bins-EOSHIFT(bins,-1)
  WRITE(*,*) 'bins = ', bins
  ! which writes  3  1  0  2  0  3
END PROGRAM test

然后,当我很高兴逻辑正确时,我会将其变成一个函数并添加一些错误检查。

如果这个问题更笼统,并且询问什么是复制Python的for-else结构的惯用性fortran(帖子90),此处也有答案。

转到语句允许任意跳跃。特别是,您写下循环,然后写下其他块,然后继续标有标签的继续。在循环中,如果条件为真,则跳到标记的标签继续。否则,for循环将正常终止,将执行其他块,然后继续执行,与Python's的语义相匹配... else construct。

例如:

        INTEGER nabsth, nvals
        PARAMETER (nabsth=5, nvals=9)
        INTEGER absth(nabsth), counts(nabsth)
        REAL vals(nvals)
        DATA absth/1, 2, 3, 4, 5/
        DATA counts/0, 0, 0, 0, 0/
        DATA vals/.1, .2, .5, 1.2, 3.5, 3.7, 16.8, 19.8, 135.60/
        do iv = 1, nvals
          do ia = 1, nabsth - 1
            if (vals(iv) < absth(ia)) then
              counts(ia) = counts(ia) + 1
              goto 10
            end if
          end do
          counts(nabsth) = counts(nabsth) + 1
10        continue
        end do
        WRITE (*,*), counts
        end

产生

       3           1           0           2           3

由于仅在处理所有元素时才能执行Python的For-Else块的else部分,如何简单地将if语句用于最后一个元素?例如,

program main
    implicit none
    integer i, n
    print *, "n = ?" ; read(*,*) n
    do i = 1, 10
        if ( i <= n ) then
            print *, i
        else
            exit
        endif
        if ( i == 10 ) print *, "reached the final index"
    enddo
    print *, "done"
end program

可能对应于

n = int( input( "n = ? n" ) )
for i in range( 1, 11 ):
    if i <= n:
        print( i )
    else:
        break
else:
    print( "reached the final index" )
print( "done" )

另一种方法可能是使用标记的block构造,例如:

program main
    implicit none
    integer i, n
    print *, "n = ?" ; read(*,*) n
    loop_i : block
      do i = 1, 10
          if ( i <= n ) then
              print *, i
          else
              exit loop_i
          endif
      enddo
      print *, "reached the final index"
    endblock loop_i
    print *, "done"
end program

根据第20.1.7章:"从几乎所有构造中退出"现代Fortran中的解释(由Metcalf等人)以及F2008标准第8.1.1.10章(从此处获得),可以退出任何标记的构造,例如blockifassociate等,但是我们可能需要一个相对较新的编译器(Gfortran-6为我工作)。出口的IBM人页面也很有用。

据我所知,python是唯一具有for-else语句的(或极少数)语言。不,Fortran没有它。

有时goto很好。一个地方可能有用的地方...

do iv = 1, nvals
  is_in_last_absth = .true.
  Mask = .FALSE.
  Mask(1:(nabsth - 1)) = .TRUE.)
  Mask2 = .FALSE.
  WHERE(MASK)
    WHERE( vals(iv) < absth)
      mask2 = .TRUE.
    ENDWHERE
    WHERE(Mask2)
      Count = Count + 1
    ELSE
      LastCount = LastCount + 1
    ENDWHERE
  END WHERE
end do
count(2:(n-1)) = count(2:(n-1))+ lastcount(1:(n))

最新更新