擅长:python、mysql、java
<p>尽管有<a href="https://stackoverflow.com/a/9453650/210905">Gandaro's good advice</a>,按名称检索变量是可能的。你知道吗</p>
<p>有<a href="http://docs.python.org/library/functions.html#vars" rel="nofollow noreferrer">vars()</a>、<a href="http://docs.python.org/library/functions.html#globals" rel="nofollow noreferrer">globals()</a>和<a href="http://docs.python.org/library/functions.html#locals" rel="nofollow noreferrer">locals()</a>函数。
所以如果你想把它修好,你可以这样做:</p>
<pre><code>domesticated = ['cow', 'sheep', 'pig']
pets = ['cat', 'dog']
wildlife = ['giraffe', 'lion', 'panda']
masterList = ['domesticated', 'pets', 'wildlife'], ['off', 'on', 'on']
def currentLists():
activeList = []
for i in range(len(masterList[0])):
if masterList[1][i] == 'on':
activeList = activeList + globals()[masterList[0][i]]
return activeList
</code></pre>