Ask Your Question

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


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

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)
edit flag offensive delete link more



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


Asked: 2022-03-01 22:14:56 +0200

Seen: 131 times

Last updated: Mar 01 '22