Popular recipes by webby1111 http://code.activestate.com/recipes/users/4192908/2015-09-29T16:56:16-07:00ActiveState Code RecipesPython add/set attributes to list (Python)
2015-09-29T16:28:46-07:00webby1111http://code.activestate.com/recipes/users/4192908/http://code.activestate.com/recipes/579103-python-addset-attributes-to-list/
<p style="color: grey">
Python
recipe 579103
by <a href="/recipes/users/4192908/">webby1111</a>
(<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/subclass/">subclass</a>).
Revision 3.
</p>
<h4 id="python-attribute-listhttpsgithubcomwebby1111python-attribute-list"><a href="https://github.com/webby1111/Python-Attribute-List">Python Attribute List</a></h4>
<p>Add/set attributes to python lists.</p>
<p>A google search for "add attributes to python lists" yields no good stackoverflow answer,
hence the need for this.</p>
<p>Useful for machine learning stuff where you need labeled feature vectors. </p>
<p>This technique can be easily adapted for other built-ins (e.g. int).</p>
<h5 id="the-problem">The Problem</h5>
<pre class="prettyprint"><code>a = [1, 2, 4, 8]
a.x = "Hey!" # AttributeError: 'list' object has no attribute 'x'
</code></pre>
<h5 id="the-solution">The Solution</h5>
<pre class="prettyprint"><code>a = L(1, 2, 4, 8)
a.x = "Hey!"
print a # [1, 2, 4, 8]
print a.x # "Hey!"
print len(a) # 4
# You can also do these:
a = L( 1, 2, 4, 8 , x="Hey!" ) # [1, 2, 4, 8]
a = L( 1, 2, 4, 8 )( x="Hey!" ) # [1, 2, 4, 8]
a = L( [1, 2, 4, 8] , x="Hey!" ) # [1, 2, 4, 8]
a = L( {1, 2, 4, 8} , x="Hey!" ) # [1, 2, 4, 8]
a = L( [2 ** b for b in range(4)] , x="Hey!" ) # [1, 2, 4, 8]
a = L( (2 ** b for b in range(4)) , x="Hey!" ) # [1, 2, 4, 8]
a = L( 2 ** b for b in range(4) )( x="Hey!" ) # [1, 2, 4, 8]
a = L( 2 ) # [2]
</code></pre>
Simple Python Point KD Tree (No scipy/numpy needed) (Python)
2015-09-29T16:56:16-07:00webby1111http://code.activestate.com/recipes/users/4192908/http://code.activestate.com/recipes/579104-simple-python-point-kd-tree-no-scipynumpy-needed/
<p style="color: grey">
Python
recipe 579104
by <a href="/recipes/users/4192908/">webby1111</a>
(<a href="/recipes/tags/geometry/">geometry</a>, <a href="/recipes/tags/kdtree/">kdtree</a>).
</p>
<p>A very simple and concise KD-tree for points in python.</p>
<p>For labeled points, you may want to check out my other recipe:
<a href="https://code.activestate.com/recipes/579103-python-addset-attributes-to-list/?in=user-4192908">Python add/set attributes to list</a></p>