0

I want to know if it is possible to compare if a string is equal with a variable name. For example I have the following declaration:

S=['A']
A=[['C'],['A','c','C']]
C=[['a'],['b'],['d','D']]
D=['A','e']
M=[S,A,C,D]
temp=[]

and

temp.append(S[0])
if S[0] in M :
...

Therefore I need to check if a string is equal with a variable name. Is it possible to do this? Thanks.

3
  • Do you want to know if M contains a reference to the same list as what A refers to? Python names are mere references; what if there is a B that refers to the same list, for example? Commented Dec 21, 2013 at 15:12
  • 2
    what is exactly your use-case for this? Commented Dec 21, 2013 at 15:12
  • I need this for left/right terminal sets at Formal Language. Commented Dec 21, 2013 at 15:30

1 Answer 1

1

You'd have to derefence A first, using globals() for example:

if globals()[S[0]] in M:

However, you should rarely need to use this, however. Generally, you'd have such objects in a dictionary of your own, for example:

lists = {'A': [...], 'C': [...]}

and then you just test if S[0] in lists is True.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.