# elif in list

Is it possible to use a recursive list. My problem is that I can code a list with an if condition. But I have not found the way to insert inside a list an elif.

For instance in pur Python to code x[t] = a x[t-1] + b, x given is coded without any difficulties. But what I search is the list way as in Mathematica (<- I know my mother often told me not tell swear words).

In fact I have two problems the first is purely recursive like mathematica code

RecurrenceTable[{a[n + 1] == 3 a[n], a == 7}, a, {n, 1, 10}]


here the if condition would be some thing like

A=[a[n + 1] == 3 a[n]  for i in range(10) if n  > 1 elif : a[n] == 7 if n in range(0)]


but also

A=[something(i) for i in range(..) if condition1 on something(i) elif condition2 on something(i)]

edit retag close merge delete

Please add code that you wish would work but does not work.

From there it should be possible to explain why it does not work and how to achieve the desired result.

Or add Mathematica code and hopefully someone can translate.

From what I understand the Wolfram Language is publicly specified.

Sort by » oldest newest most voted

Still not have time to learn Python ?

• The Pythonism for the conditional operator is <value> if <condition> else <alternative value>.No elif, but that's not an hindrance (elifis syntactic sugar anyway...).

Example :

sage: [-1 if u<0 else 0 if u==0 else +1 for u in range(-2,3)]
[-1, -1, 0, 1, 1]

• Since a lambda function is, by definition, anonymous, there is, as far as I can tell, no way to "cleany" have it call itself.

There are some proposals to do this in contorted ways (amounting to recursively create, use and discard new anonymous functions), which I find difficult to understand in vivo and probably impossible to debug.

BTW, aping each and every Mathematica function is probably not a good way to learn Sage ; if you insist on modeling your code on a non-Python language, Lisp would be a much better example...

more