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 +0200

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 +0200

rburing gravatar image

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

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 +0200 )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 +0200

Seen: 34 times

Last updated: Mar 01