Ask Your Question
0

Indexing variables in a list comprehension

asked 2020-12-01 23:03:40 +0100

Laughematician760 gravatar image

Suppose I create the polynomial ring R = PolynomialRing(QQ, ['lambda%s'%i for i in [1 .. g]] + ['psi%s'%i for i in [1 .. n]]).

If I want to create a list comprehension which creates a list of perhaps all the lambdas, what is the notation used at the beginning of the list comprehension?

i.e. [lambdai for i in [1 .. g]].

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-12-02 00:58:10 +0100

rburing gravatar image

You can use R.gen(i-1), but it takes fewer keystrokes to slice the list of generators:

sage: g = 3; n = 4
sage: R = PolynomialRing(QQ, ['lambda%s'%i for i in [1 .. g]] + ['psi%s'%i for i in [1 .. n]])
sage: lambdas = R.gens()[:g]
sage: psis = R.gens()[n-1:]
sage: lambdas, psis
((lambda1, lambda2, lambda3), (psi1, psi2, psi3, psi4))

Another trick is to start your indexing at 0 so that you can name your list like psi and then psi[0] will refer to the variable named psi0. Or you can shift your list, or make a dictionary, if you want indices to start at 1.

edit flag offensive delete link more

Comments

1

Unfortunately you can't name your list lambda.

John Palmieri gravatar imageJohn Palmieri ( 2020-12-02 06:38:27 +0100 )edit

Thank you!

Laughematician760 gravatar imageLaughematician760 ( 2020-12-02 18:16:31 +0100 )edit

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-12-01 23:03:40 +0100

Seen: 351 times

Last updated: Dec 02 '20