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 Mar 2022 01:53:16 +0100How to construct a condition on the family name of a variable without notice to its indexhttps://ask.sagemath.org/question/61344/how-to-construct-a-condition-on-the-family-name-of-a-variable-without-notice-to-its-index/ Suppose I have the list
`R=['$x_{0}$','$x_{1}$','$x_{2}$','$a_{3}$','$\\varepsilon_{4}$','$a_{5}$','$\\varepsilon_{6}$'`]
how can I make a condition looking like
if R[i]== a (the index is not relevant only the family name of the variable)Tue, 01 Mar 2022 22:14:56 +0100https://ask.sagemath.org/question/61344/how-to-construct-a-condition-on-the-family-name-of-a-variable-without-notice-to-its-index/Answer by rburing for <p>Suppose I have the list</p>
<p><code>R=['$x_{0}$','$x_{1}$','$x_{2}$','$a_{3}$','$\\varepsilon_{4}$','$a_{5}$','$\\varepsilon_{6}$'</code>]</p>
<p>how can I make a condition looking like</p>
<p>if R[i]== a (the index is not relevant only the family name of the variable)</p>
https://ask.sagemath.org/question/61344/how-to-construct-a-condition-on-the-family-name-of-a-variable-without-notice-to-its-index/?answer=61345#post-id-61345If `v` is in `R` then you can use e.g. the condition `v[:3] == '$a_'` to check that the first three characters in `v` are `$a_`.
sage: [v for v in R if v[:3] == '$a_']
['$a_{3}$', '$a_{5}$']
sage: for v in R:
....: if v[:3] == '$a_':
....: print(v)
....:
$a_{3}$
$a_{5}$Tue, 01 Mar 2022 22:30:29 +0100https://ask.sagemath.org/question/61344/how-to-construct-a-condition-on-the-family-name-of-a-variable-without-notice-to-its-index/?answer=61345#post-id-61345Comment by John Palmieri for <p>If <code>v</code> is in <code>R</code> then you can use e.g. the condition <code>v[:3] == '$a_'</code> to check that the first three characters in <code>v</code> are <code>$a_</code>.</p>
<pre><code>sage: [v for v in R if v[:3] == '$a_']
['$a_{3}$', '$a_{5}$']
sage: for v in R:
....: if v[:3] == '$a_':
....: print(v)
....:
$a_{3}$
$a_{5}$
</code></pre>
https://ask.sagemath.org/question/61344/how-to-construct-a-condition-on-the-family-name-of-a-variable-without-notice-to-its-index/?comment=61347#post-id-61347Or you can use the condition `v.startswith('$a_')` if you don't feel like counting to 3.Wed, 02 Mar 2022 01:53:16 +0100https://ask.sagemath.org/question/61344/how-to-construct-a-condition-on-the-family-name-of-a-variable-without-notice-to-its-index/?comment=61347#post-id-61347