Browse Source

Iterator

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

7
README.md

@ -88,7 +88,7 @@ value = <dict>.setdefault(key, default=None) # Returns and writes default if
```python
value = <dict>.pop(key) # Removes item or raises KeyError.
<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 for k, v in <dict>.items() if v == value] # Returns list of keys that point to the value.
{k: v for k, v in <dict>.items() if k in keys} # Returns dictionary filtered by keys.
```
@ -194,8 +194,10 @@ Iterator
<iter> = iter(<collection>) # `iter(<iter>)` returns unmodified iterator.
<iter> = iter(<function>, to_exclusive) # A sequence of return values until 'to_exclusive'.
<el> = next(<iter> [, default]) # Raises StopIteration or returns 'default' on end.
<list> = list(<iter>) # Returns a list of iterator's remaining elements.
```
### Itertools
```python
from itertools import count, repeat, cycle, chain, islice
@ -214,8 +216,7 @@ from itertools import count, repeat, cycle, chain, islice
```python
<iter> = islice(<collection>, to_exclusive)
<iter> = islice(<collection>, from_inclusive, to_exclusive)
<iter> = islice(<collection>, from_inclusive, to_exclusive, +step_size)
<iter> = islice(<collection>, from_inclusive, to_exclusive [, +step_size])
```

6
index.html

@ -272,7 +272,7 @@ value = &lt;dict&gt;.setdefault(key, default=<span class="hljs-keyword">None</s
</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>
&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 <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 the 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
@ -344,6 +344,7 @@ to_exclusive = &lt;range&gt;.stop
<div><h2 id="iterator"><a href="#iterator" name="iterator">#</a>Iterator</h2><pre><code class="python language-python hljs">&lt;iter&gt; = iter(&lt;collection&gt;) <span class="hljs-comment"># `iter(&lt;iter&gt;)` returns unmodified iterator.</span>
&lt;iter&gt; = iter(&lt;function&gt;, to_exclusive) <span class="hljs-comment"># A sequence of return values until 'to_exclusive'.</span>
&lt;el&gt; = next(&lt;iter&gt; [, default]) <span class="hljs-comment"># Raises StopIteration or returns 'default' on end.</span>
&lt;list&gt; = list(&lt;iter&gt;) <span class="hljs-comment"># Returns a list of iterator's remaining elements.</span>
</code></pre></div>
<div><h3 id="itertools">Itertools</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> count, repeat, cycle, chain, islice
@ -357,8 +358,7 @@ to_exclusive = &lt;range&gt;.stop
&lt;iter&gt; = chain.from_iterable(&lt;collection&gt;) <span class="hljs-comment"># Empties collections inside a collection in order.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;iter&gt; = islice(&lt;collection&gt;, to_exclusive)
&lt;iter&gt; = islice(&lt;collection&gt;, from_inclusive, to_exclusive)
&lt;iter&gt; = islice(&lt;collection&gt;, from_inclusive, to_exclusive, +step_size)
&lt;iter&gt; = islice(&lt;collection&gt;, from_inclusive, to_exclusive [, +step_size])
</code></pre>
<div><h2 id="generator"><a href="#generator" name="generator">#</a>Generator</h2><ul>
<li><strong>Any function that contains a yield statement returns a generator.</strong></li>

Loading…
Cancel
Save