是否有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