<p>Python的<a href="http://docs.python.org/2/tutorial/interpreter.html#interactive-mode" rel="nofollow noreferrer">interactive mode</a>是逐步探索结构化数据的好方法。很容易找到如何访问silvermoon服务器数据:</p>
<pre><code>>>> data=json.load(urllib2.urlopen("http://eu.battle.net/api/wow/realm/status"))
>>> type(data)
<type 'dict'>
>>> data.keys()
[u'realms']
>>> type(data['realms'])
<type 'list'>
>>> type(data['realms'][0])
<type 'dict'>
>>> data['realms'][0].keys()
[u'status', u'wintergrasp', u'battlegroup', u'name', u'tol-barad', u'locale', u'queue', u'timezone', u'type', u'slug', u'population']
>>> data['realms'][0]['name']
u'Aegwynn'
>>> [realm['name'] for realm in data['realms']].index('Silvermoon')
212
>>> silvermoon= data['realms'][212]
>>> silvermoon['population']
u'high'
>>> type(silvermoon['wintergrasp'])
<type 'dict'>
>>> silvermoon['wintergrasp'].keys()
[u'status', u'next', u'controlling-faction', u'area']
>>> silvermoon['wintergrasp']['controlling-faction']
>>> silvermoon['population']
u'high'
</code></pre>
<p>如果你还不知道它们,你应该阅读<a href="http://docs.python.org/2/tutorial/datastructures.html#dictionaries" rel="nofollow noreferrer">dictionary.keys</a>、<a href="http://docs.python.org/2/tutorial/datastructures.html#more-on-lists" rel="nofollow noreferrer">list.index</a>和<a href="http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions" rel="nofollow noreferrer">list comprehensions</a>来了解发生了什么。</p>
<p>在确定了数据的结构之后,您最终可以重写数据访问,使其更具可读性和效率:</p>
<pre><code>realms= data['realms']
realm_from_name= dict( [(realm['name'], realm) for realm in realms])
print realm_from_name['Silvermoon']['population']
print realm_from_name['Silvermoon']['wintergrasp']['controlling-faction']
</code></pre>
<p>至于将数组复制到另一个变量是浪费,您应该知道python传递值<a href="http://en.wikipedia.org/wiki/Reference_%28computer_science%29" rel="nofollow noreferrer">by reference</a>。这意味着当你给一个新变量赋值时不需要复制。<a href="https://stackoverflow.com/a/430958/1595865">Here's a simple explanation of passing by value vs passing by reference</a></p>
<p>最后,你似乎过分担心表现。Python的哲学是<a href="https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Overview:_Optimize_what_needs_optimizing" rel="nofollow noreferrer">get it right first, optimize later</a>。<em>当</em>工作正常时,<em>如果</em>需要更好的性能,则优化它(如果值得的话)。</p>