Construct vectors or lists with 'for', 'if' and 'else'
I'm trying to generate some vectors (or lists) recursively, but I'm getting some trouble in the procedure:
def p(*t):
return SR.var(('p' + '_{}' * len(t)).format(*t))
l= Permutations(3)
for i in (1..3):
for u in (1..3):
p(i,u) = [1 for m in (0..5) if l[m][i-1]==u else 0]
This code below returns 'invalid syntax'. How can I generate the list p's with entrance equal to one if l[m][i-1]=u and zero in the rest? Can I generate a matrix with the same idea?
I think you cannot use
else
whenif
comes afterfor
in the list comprehension. Try[ int( l[m][i-1]==u ) for m in (0..5) ]
instead.