ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 13 Feb 2021 19:06:24 +0100Conditional in listhttps://ask.sagemath.org/question/55696/conditional-in-list/If this works
[1 if i<12 else (0 if 12<= i <=15 else 2) for i in range(20)]
where is my fault with
cand=["A","B","C","D"]
cond1=[[141,107],[163,85],[127,121],[132,116],[117,131],[120,128]]
cond2=[(["A","B"],"A"),(["A","C"],"A"),(["A","D"],"A"),(["B","C"],"B"),(["B","D"],"D"),(["C","D"],"D")]
zoo=[[0 if cond2[i][1]==cand[j] else ((abs(cond1[i][0]-cond1[i][1])) if cond2[i][0][0]=cand[j] or cond2[i][0][1]=cand[j]) for i in range(len(cond2))] for j in range(len(cand))]
zooSat, 13 Feb 2021 18:55:13 +0100https://ask.sagemath.org/question/55696/conditional-in-list/Answer by Max Alekseyev for <p>If this works</p>
<pre><code>[1 if i<12 else (0 if 12<= i <=15 else 2) for i in range(20)]
</code></pre>
<p>where is my fault with</p>
<pre><code>cand=["A","B","C","D"]
cond1=[[141,107],[163,85],[127,121],[132,116],[117,131],[120,128]]
cond2=[(["A","B"],"A"),(["A","C"],"A"),(["A","D"],"A"),(["B","C"],"B"),(["B","D"],"D"),(["C","D"],"D")]
zoo=[[0 if cond2[i][1]==cand[j] else ((abs(cond1[i][0]-cond1[i][1])) if cond2[i][0][0]=cand[j] or cond2[i][0][1]=cand[j]) for i in range(len(cond2))] for j in range(len(cand))]
zoo
</code></pre>
https://ask.sagemath.org/question/55696/conditional-in-list/?answer=55697#post-id-55697You don't have `else` for the second `if`.Sat, 13 Feb 2021 19:06:24 +0100https://ask.sagemath.org/question/55696/conditional-in-list/?answer=55697#post-id-55697