Browse Source

Dict

pull/46/head
Jure Šorn 5 years ago
parent
commit
7db86add20
2 changed files with 7 additions and 5 deletions
  1. 5
      README.md
  2. 7
      index.html

5
README.md

@ -80,7 +80,6 @@ value = <dict>.setdefault(key, default=None) # Returns and writes default if
```
```python
<dict>.update(<dict>)
<dict> = dict(<collection>) # Creates a dict from coll. of key-value pairs.
<dict> = dict(zip(keys, values)) # Creates a dict from two collections.
<dict> = dict.fromkeys(keys [, value]) # Creates a dict from collection of keys.
@ -88,7 +87,9 @@ value = <dict>.setdefault(key, default=None) # Returns and writes default if
```python
value = <dict>.pop(key) # Removes item or raises KeyError.
{k: v for k, v in <dict>.items() if k in keys} # Filters dictionary by keys.
<dict>.update(<dict>) # Adds items. Replaces ones with matching keys.
[k for k, v in <dict>.items() if v == value] # Returns list of keys that point to value.
{k: v for k, v in <dict>.items() if k in keys} # Returns dictionary filtered by keys.
```
### Counter

7
index.html

@ -266,13 +266,14 @@ value = &lt;dict&gt;.setdefault(key, default=<span class="hljs-keyword">None</s
&lt;dict&gt; = collections.defaultdict(&lt;type&gt;) <span class="hljs-comment"># Creates a dict with default value of type.</span>
&lt;dict&gt; = collections.defaultdict(<span class="hljs-keyword">lambda</span>: <span class="hljs-number">1</span>) <span class="hljs-comment"># Creates a dict with default value 1.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;dict&gt;.update(&lt;dict&gt;)
&lt;dict&gt; = dict(&lt;collection&gt;) <span class="hljs-comment"># Creates a dict from coll. of key-value pairs.</span>
<pre><code class="python language-python hljs">&lt;dict&gt; = dict(&lt;collection&gt;) <span class="hljs-comment"># Creates a dict from coll. of key-value pairs.</span>
&lt;dict&gt; = dict(zip(keys, values)) <span class="hljs-comment"># Creates a dict from two collections.</span>
&lt;dict&gt; = dict.fromkeys(keys [, value]) <span class="hljs-comment"># Creates a dict from collection of keys.</span>
</code></pre>
<pre><code class="python language-python hljs">value = &lt;dict&gt;.pop(key) <span class="hljs-comment"># Removes item or raises KeyError.</span>
{k: v <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> &lt;dict&gt;.items() <span class="hljs-keyword">if</span> k <span class="hljs-keyword">in</span> keys} <span class="hljs-comment"># Filters dictionary by keys.</span>
&lt;dict&gt;.update(&lt;dict&gt;) <span class="hljs-comment"># Adds items. Replaces ones with matching keys.</span>
[k <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> &lt;dict&gt;.items() <span class="hljs-keyword">if</span> v == value] <span class="hljs-comment"># Returns list of keys that point to value.</span>
{k: v <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> &lt;dict&gt;.items() <span class="hljs-keyword">if</span> k <span class="hljs-keyword">in</span> keys} <span class="hljs-comment"># Returns dictionary filtered by keys.</span>
</code></pre>
<div><h3 id="counter">Counter</h3><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> Counter
<span class="hljs-meta">&gt;&gt;&gt; </span>colors = [<span class="hljs-string">'blue'</span>, <span class="hljs-string">'red'</span>, <span class="hljs-string">'blue'</span>, <span class="hljs-string">'red'</span>, <span class="hljs-string">'blue'</span>]

Loading…
Cancel
Save