Python强制转换和解析变量的值

Python强制转换和解析变量的值,python,Python,我在一个变量中有一个列表的名称,比如list_name='listA' 我怎样才能在这个列表中循环,就像一个演员 e、 g Thanxs试试这个: >>> listA = ['foo', 'bar'] >>> list_name = 'listA' >>> for item in globals()[list_name]: print(item) foo bar >>> listA = ['foo', 'b

我在一个变量中有一个列表的名称,比如list_name='listA' 我怎样才能在这个列表中循环,就像一个演员

e、 g

Thanxs

试试这个:

>>> listA = ['foo', 'bar']
>>> list_name = 'listA'
>>> for item in globals()[list_name]:
        print(item)
foo
bar
>>> listA = ['foo', 'bar']
>>> list_name = 'listA'
>>> for item in globals()[list_name]:
        print(item)
foo
bar