What is the equivalent of the following Python list comprehension in Fortran? -
i trying write following list comprehension(written in python) in fortran.
lit = [[x,y] x in [p,q,r] y in [h,k,l] if [x,y]!=[a,b]]
where a, b, p ,q ,r, h, k, l
integers
how can achieve if want fill columns first in 2d fortran array?
the python code returns list. equivalent to
for x in [p,q,r]: y in [h,k,l]: if [x,y]!=[a,b]: list.append([x,y])
i made 2 sublists in fortran. sublist_x
, sublist_y
each list contains p,q,r
, h,k,l
respectively.
integer :: list(0:7), sublist_x(0:2),sublist_y(0:2), count count =-1 i=0,7 if (i%3 ==0) count = count +1 endif list(0,i)=sublist_x(i%3) list(1,i)=sublist_y(count%3) enddo
i think complex way of doing things...
if understand correctly want cartesian product of 2 little lists, excluding element [a,b]
? if misunderstand, stop reading now. here's little program want ...
program test implicit none integer, dimension(:), allocatable :: listx, listy, bad_element integer, dimension(:,:), allocatable :: outlist integer :: ix, jx, alstat, n_elements logical, dimension(:), allocatable :: rows listx = [1,2,3] listy = [21,22,23] bad_element = [3,21] n_elements = size(listx)*size(listy) allocate(outlist(2,n_elements),stat=alstat) if (alstat/=0) write(*,*) "something went wrong allocating result array" stop else outlist(1,:) = reshape(listx,[n_elements],listx) outlist(2,:) = reshape(spread(listy,1,size(listx)),[n_elements]) end if ix = 1, n_elements if (all(outlist(:,ix)==bad_element)) outlist(:,ix:) = eoshift(outlist(:,ix:),1,dim=2) end if end end program test
at end of program outlist
contains cartesian product elements equal bad element replaced 0
s , pushed end of outlets
. hard-wired numbers above, output is:
1 2 1 2 3 1 2 3 0 21 21 22 22 22 23 23 23 0
i guess shouldn't have difficulty trimming remove 0
s, nor in packaging program routine. , hope code explains itself.
Comments
Post a Comment