|
249 | 249 | product_of_elems = functools.reduce(<span class="hljs-keyword">lambda</span> out, x: out * x, <collection>)
|
250 | 250 | list_of_chars = list(<str>)
|
251 | 251 | </code></pre>
|
| 252 | +<ul> |
| 253 | +<li><strong>Check out module <a href="#operator">operator</a> for alternative versions of examples.</strong></li> |
| 254 | +</ul> |
252 | 255 | <pre><code class="python language-python hljs"><int> = <list>.count(<el>) <span class="hljs-comment"># Returns number of occurrences. Also works on strings.</span>
|
253 | 256 | index = <list>.index(<el>) <span class="hljs-comment"># Returns index of first occurrence or raises ValueError.</span>
|
254 | 257 | <list>.insert(index, <el>) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span>
|
|
1853 | 1856 |
|
1854 | 1857 |
|
1855 | 1858 | <pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> operator <span class="hljs-keyword">as</span> op
|
1856 |
| -product_of_elems = functools.reduce(op.mul, <collection>) |
1857 | 1859 | elementwise_sum = map(op.add, list_a, list_b)
|
1858 | 1860 | sorted_by_second = sorted(<collection>, key=op.itemgetter(<span class="hljs-number">1</span>))
|
1859 | 1861 | sorted_by_both = sorted(<collection>, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>))
|
| 1862 | +product_of_elems = functools.reduce(op.mul, <collection>) |
1860 | 1863 | LogicOp = enum.Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: op.and_, <span class="hljs-string">'OR'</span> : op.or_})
|
1861 | 1864 | last_el = op.methodcaller(<span class="hljs-string">'pop'</span>)(<list>)
|
1862 | 1865 | </code></pre>
|
1863 |
| -<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs"><list> = dir() <span class="hljs-comment"># Returns names of local variables (including functions).</span> |
| 1866 | +<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs"><list> = dir() <span class="hljs-comment"># Returns names of local variables (incl. functions).</span> |
1864 | 1867 | <dict> = vars() <span class="hljs-comment"># Returns dict of local variables. Also locals().</span>
|
1865 | 1868 | <dict> = globals() <span class="hljs-comment"># Returns dict of global variables.</span>
|
1866 | 1869 | </code></pre></div></div>
|
1867 | 1870 |
|
1868 | 1871 |
|
1869 | 1872 |
|
1870 | 1873 | <div><h3 id="attributes-1">Attributes</h3><pre><code class="python language-python hljs"><list> = dir(<object>) <span class="hljs-comment"># Returns names of object's attributes (incl. methods).</span>
|
1871 |
| -<dict> = vars(<object>) <span class="hljs-comment"># Returns dict of object's fields. Also <object>.__dict__.</span> |
| 1874 | +<dict> = vars(<object>) <span class="hljs-comment"># Returns dict of object's fields. Also <obj>.__dict__.</span> |
1872 | 1875 | </code></pre></div>
|
1873 | 1876 |
|
1874 | 1877 | <pre><code class="python language-python hljs"><bool> = hasattr(<object>, <span class="hljs-string">'<attr_name>'</span>)
|
|
0 commit comments