Ask Your Question
0

How to construct a condition on the family name of a variable without notice to its index

asked 2022-03-01 22:14:56 +0100

Cyrille gravatar image

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)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-03-01 22:30:29 +0100

rburing gravatar image

updated 2022-03-01 22:33:04 +0100

If 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}$
edit flag offensive delete link more

Comments

1

Or you can use the condition v.startswith('$a_') if you don't feel like counting to 3.

John Palmieri gravatar imageJohn Palmieri ( 2022-03-02 01:53:16 +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: 2022-03-01 22:14:56 +0100

Seen: 179 times

Last updated: Mar 01 '22