擅长:python、mysql、java
<blockquote>
<p>How can I keep it as simple as possible and efficiently to find and get the value of a dict without needing to use alot of resources and if its not found then return a default value?</p>
</blockquote>
<p>这个问题的答案是使用<a href="https://docs.python.org/3/library/collections.html#collections.defaultdict" rel="nofollow noreferrer">^{<cd1>}</a>:</p>
<pre class="lang-py prettyprint-override"><code>>>> import collections
>>> d = collections.defaultdict(list, {'key1': [1], 'key2': [2]})
>>> d['key1']
[1]
>>> d['key3']
[]
</code></pre>