Latest recipes by Garrett Code RecipesCoordinates of numpy array from index and shape (Python) 2012-10-23T19:51:55-07:00Garrett <p style="color: grey"> Python recipe 578302 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/coordinates/">coordinates</a>, <a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>returns the coordinates of a numpy array given the index and the shape. A first_index_et function is given as example code</p> pygmail (can send mail) (Python) 2012-07-10T01:29:42-07:00Garrett <p style="color: grey"> Python recipe 578203 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/gmail/">gmail</a>, <a href="/recipes/tags/google/">google</a>, <a href="/recipes/tags/mail/">mail</a>, <a href="/recipes/tags/pop/">pop</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Can both send and receive mail, as well as do other things.</p> Working with Excel Indexes (Python) 2012-05-22T18:25:21-07:00Garrett <p style="color: grey"> Python recipe 578142 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/column/">column</a>, <a href="/recipes/tags/columns/">columns</a>, <a href="/recipes/tags/excel/">excel</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>I have had extensive issues working with excel indexies (i.e. 'AA100'). Here is code to convert out of and into all of the indexes.</p> <p>There may be better ways to do this, I don't care anymore. This works.</p> Flattening an arbitrarily deep list (or any iterator) (Python) 2012-04-03T17:13:35-07:00Garrett <p style="color: grey"> Python recipe 578092 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tuple/">tuple</a>). Revision 6. </p> <p>What if you had a list like this: [1, -10, [1,2,[3,4]], xrange(200)], and you just wanted to go through each element in order (wanted it to return a simple list of [1,-10,1,2,3,4,1,2,3,4...199])</p> <p>I've seen a couple of attempts to flatten arbitrarily deep lists. Many of them involve recursion, like this one: <a href="" rel="nofollow"></a></p> <p>Recursion is generally considered non-pythonic (at least to my knowledge), so I have used one which just involves simple iterators instead. Also, recursion will fail if the list is too deep (so it wouldn't really be arbitrary, would it?).</p> Fast Sorting of n dimensional array by first dimension (Python) 2012-03-14T15:14:11-07:00Garrett <p style="color: grey"> Python recipe 578074 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/argsort/">argsort</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p>I have looked far and wide for code for fast sorting of n dimensional arrays by the first element, for example if I had the array: <br /> ray = [[1,2,3,7,5][10,11,12,13,14]] </p> <p>I would want it to come out as ray = [[1,2,3,5,7][10,11,12,14,13]]</p> <p>There are several ways to do this. One is zipped = zip(<em>ray) zipped.sort() ray = zip(</em>zipped)</p> <p>but this is extremely slow. Numpy has a much faster way to do it, but it wasn't immediately apparent.</p> <p>if the above were a numpy array you could simply do the following: indexes = numpy.argsort(ray[0]) for n in xrange(len(ray)) ray[n] = ray[n][indexes]</p> <p>I did a time test of the two methods below.</p> Fast Indexing functions (greater than, less than, equal to, and not equal to) (Python) 2012-03-13T16:21:36-07:00Garrett <p style="color: grey"> Python recipe 578071 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/list_comprehension/">list_comprehension</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>Oftentimes you want to find the index of a list-like object. Numpy arrays, for example, do not have a index member function. These get the job done quickly.</p> <p>Note: these do not raise exceptions, instead they return -1 on error. You should change that if you want different behavior.</p> extending xrange to support slicing and indexing (Python) 2012-03-13T16:13:16-07:00Garrett <p style="color: grey"> Python recipe 578070 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/xrange/">xrange</a>). </p> <p>an object which extends the xrange object to support slicing and indexing (simple)</p>