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.Wed, 02 Dec 2020 18:16:31 +0100Indexing variables in a list comprehensionhttps://ask.sagemath.org/question/54503/indexing-variables-in-a-list-comprehension/ 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]]. Tue, 01 Dec 2020 23:03:40 +0100https://ask.sagemath.org/question/54503/indexing-variables-in-a-list-comprehension/Answer by rburing for <p>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]]).</p>
<p>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?</p>
<p>i.e. [lambdai for i in [1 .. g]]. </p>
https://ask.sagemath.org/question/54503/indexing-variables-in-a-list-comprehension/?answer=54505#post-id-54505You 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.Wed, 02 Dec 2020 00:58:10 +0100https://ask.sagemath.org/question/54503/indexing-variables-in-a-list-comprehension/?answer=54505#post-id-54505Comment by John Palmieri for <p>You can use <code>R.gen(i-1)</code>, but it takes fewer keystrokes to slice the list of generators:</p>
<pre><code>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))
</code></pre>
<p>Another trick is to start your indexing at <code>0</code> so that you can name your list like <code>psi</code> and then <code>psi[0]</code> will refer to the variable named <code>psi0</code>. Or you can shift your list, or make a dictionary, if you want indices to start at 1.</p>
https://ask.sagemath.org/question/54503/indexing-variables-in-a-list-comprehension/?comment=54508#post-id-54508Unfortunately you can't name your list `lambda`.Wed, 02 Dec 2020 06:38:27 +0100https://ask.sagemath.org/question/54503/indexing-variables-in-a-list-comprehension/?comment=54508#post-id-54508Comment by Laughematician760 for <p>You can use <code>R.gen(i-1)</code>, but it takes fewer keystrokes to slice the list of generators:</p>
<pre><code>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))
</code></pre>
<p>Another trick is to start your indexing at <code>0</code> so that you can name your list like <code>psi</code> and then <code>psi[0]</code> will refer to the variable named <code>psi0</code>. Or you can shift your list, or make a dictionary, if you want indices to start at 1.</p>
https://ask.sagemath.org/question/54503/indexing-variables-in-a-list-comprehension/?comment=54525#post-id-54525Thank you!Wed, 02 Dec 2020 18:16:31 +0100https://ask.sagemath.org/question/54503/indexing-variables-in-a-list-comprehension/?comment=54525#post-id-54525