是否有Fortran等价于Python的for-else语句?是否有Fortran等价于Python的for-else语句?
例如,以下内容将数字列表分类到不同的范围。在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与NumPy和Fortran在表现力和功能方面非常相似。这罗塞塔石显示如何实施两种语言的许多常见习语并排。 http://www.fortran90.org/src/rosetta.html – jlokimlin