Ask Your Question
-1

elif in list

asked 2020-10-30 03:24:03 -0600

Cyrille gravatar image

updated 2020-10-30 11:18:55 -0600

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[0] 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[1] == 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 flag offensive close merge delete

Comments

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.

slelievre gravatar imageslelievre ( 2020-10-30 04:53:48 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-10-30 12:40:27 -0600

Emmanuel Charpentier gravatar image

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...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-10-30 03:24:03 -0600

Seen: 57 times

Last updated: Oct 30