Popular recipes tagged "list" but not "theory"http://code.activestate.com/recipes/tags/list-theory/2017-03-03T14:13:14-08:00ActiveState Code RecipesTkinter draggable list V2 (Python) 2017-03-03T14:13:14-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580748-tkinter-draggable-list-v2/ <p style="color: grey"> Python recipe 580748 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/draggable/">draggable</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 3. </p> <p>This version use tkinter frame instead of ttk frame.</p> <p>This next link is version 1 (It's the same code but it uses ttk frame):</p> <p><a href="https://code.activestate.com/recipes/580717-sortable-megawidget-in-tkinter-like-the-sortable-w/" rel="nofollow">https://code.activestate.com/recipes/580717-sortable-megawidget-in-tkinter-like-the-sortable-w/</a></p> Python 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> Sortable megawidget in tkinter (like the sortable widget in jquery UI) or draggable list (Python) 2017-03-03T14:11:42-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580717-sortable-megawidget-in-tkinter-like-the-sortable-w/ <p style="color: grey"> Python recipe 580717 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/draggable/">draggable</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/megawidget/">megawidget</a>, <a href="/recipes/tags/sortable/">sortable</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 13. </p> <p>Sortable element list for tkinter using drag and drop.</p> <p>This version use themed Tk. My other version doesn't use ttk:</p> <p><a href="https://code.activestate.com/recipes/580748-tkinter-draggable-list-v2/" rel="nofollow">https://code.activestate.com/recipes/580748-tkinter-draggable-list-v2/</a></p> Flattening an arbitrarily nested list in Python (Python) 2014-10-05T21:44:45-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578948-flattening-an-arbitrarily-nested-list-in-python/ <p style="color: grey"> Python recipe 578948 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a recipe to flatten a Python list which may have nested lists as items within it. It works for lists that have a maximum depth of nesting roughly equal to the recursion depth limit of Python, which is set to 1000 by default, though it can be increased with sys.setrecursionlimit().</p> Fixed Lengh List (Python) 2014-01-24T15:27:25-08:00Hirohttp://code.activestate.com/recipes/users/4184239/http://code.activestate.com/recipes/578813-fixed-lengh-list/ <p style="color: grey"> Python recipe 578813 by <a href="/recipes/users/4184239/">Hiro</a> (<a href="/recipes/tags/length/">length</a>, <a href="/recipes/tags/list/">list</a>). Revision 2. </p> <p>In some applications, it's advantageous to be able to define a fixed length list.</p> <p>The class provides all features as python internal type: list</p> <p>The main focus of <strong>fixed length</strong> list is only keep certain number of items. "overflow" items will be discarded.</p> Dynamically format a comment block (Python) 2013-08-31T08:14:10-07:00Michael Swartzhttp://code.activestate.com/recipes/users/4187428/http://code.activestate.com/recipes/578624-dynamically-format-a-comment-block/ <p style="color: grey"> Python recipe 578624 by <a href="/recipes/users/4187428/">Michael Swartz</a> (<a href="/recipes/tags/comments/">comments</a>, <a href="/recipes/tags/for_loop/">for_loop</a>, <a href="/recipes/tags/list/">list</a>). Revision 3. </p> <p>Neatly format a comment block that is surrounded by #s and takes into account right side space padding.</p> An analogue of enumerate for nested lists. (Python) 2012-12-11T16:59:14-08:00John Crichtonhttp://code.activestate.com/recipes/users/4181975/http://code.activestate.com/recipes/578376-an-analogue-of-enumerate-for-nested-lists/ <p style="color: grey"> Python recipe 578376 by <a href="/recipes/users/4181975/">John Crichton</a> (<a href="/recipes/tags/enumerate/">enumerate</a>, <a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/list/">list</a>). Revision 6. </p> <p>A generator function which enables traversal and modification of deeply nested lists. Together with the supplied helper functions it could be useful when working with data stored in deeply nested lists, particularly when the level of nesting precludes a recursive approach.</p> List comparison, difference and more using set & frozenset (Python) 2012-11-01T10:30:58-07:00Scott S-Allenhttp://code.activestate.com/recipes/users/4181178/http://code.activestate.com/recipes/578310-list-comparison-difference-and-more-using-set-froz/ <p style="color: grey"> Python recipe 578310 by <a href="/recipes/users/4181178/">Scott S-Allen</a> (<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/contain/">contain</a>, <a href="/recipes/tags/difference/">difference</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/remove/">remove</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/union/">union</a>). </p> <p>Python has a powerful suite of tools for comparing lists by way of sets and frozensets. Here are a few examples and conveniences that many newcomers, even a few seasoned developers, are unaware.</p> Find Multiple Elements In a List (Python) 2011-11-05T23:40:41-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577943-find-multiple-elements-in-a-list/ <p style="color: grey"> Python recipe 577943 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/enumerate/">enumerate</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/search_list/">search_list</a>). </p> <p>This algorithm searches for more than one element in a list. The input is a list that you want to search through and a list of elements that you want to search for. The output is a multidimensional list with the positions of the elements you are searching for in the search list in the order that you listed them. </p> Geocoding Lists via Google Maps (Python) 2012-05-11T05:06:27-07:00Mano Bastardohttp://code.activestate.com/recipes/users/4182040/http://code.activestate.com/recipes/578126-geocoding-lists-via-google-maps/ <p style="color: grey"> Python recipe 578126 by <a href="/recipes/users/4182040/">Mano Bastardo</a> (<a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/coordinates/">coordinates</a>, <a href="/recipes/tags/geocode/">geocode</a>, <a href="/recipes/tags/geocoding/">geocoding</a>, <a href="/recipes/tags/google/">google</a>, <a href="/recipes/tags/google_maps/">google_maps</a>, <a href="/recipes/tags/lat/">lat</a>, <a href="/recipes/tags/latitude/">latitude</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/list_comprehension/">list_comprehension</a>, <a href="/recipes/tags/lng/">lng</a>, <a href="/recipes/tags/longitude/">longitude</a>, <a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/web/">web</a>). Revision 2. </p> <p>A simple script written as an experiment in geocoding addresses in a database. A list of addresses in the form of "100 Any Street, Anytown, CA, 10010" is passed to a Google Maps URL, and the latitude/longitude coordinates are extracted from the returned XML.</p> <p>XML methods are not used in this script, but simple string searches instead.</p> Flattening an arbitrarily deep list (or any iterator) (Python) 2012-04-03T17:13:35-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578092-flattening-an-arbitrarily-deep-list-or-any-iterato/ <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="http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html" rel="nofollow">http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html</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 Indexing functions (greater than, less than, equal to, and not equal to) (Python) 2012-03-13T16:21:36-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578071-fast-indexing-functions-greater-than-less-than-equ/ <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> METACLASS AND DEEPCOPY (Python) 2012-02-26T15:43:01-08:00thonpyhttp://code.activestate.com/recipes/users/4181053/http://code.activestate.com/recipes/578053-metaclass-and-deepcopy/ <p style="color: grey"> Python recipe 578053 by <a href="/recipes/users/4181053/">thonpy</a> (<a href="/recipes/tags/deepcopy/">deepcopy</a>, <a href="/recipes/tags/equal/">equal</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/sign/">sign</a>). </p> <p>I would like to solve this excercise: Traditionally object-oriented programming provides two different modes for cloning an instance or a structured data type: shallow and deep copy. The former has the effect to clone exclusively the external shell and not its content originating a quite fastidious aliasing effect. The latter, instead, copies the shell and its content recursively creating two completely separate copies of the instance.</p> <p>As you know, Python's programs suffer of the aliasing effect when you copy an instance of a class or a structured type (e.g., a list) with the = operator. As showed in the following example:</p> <p>l=[0,1,2] mylist=l mylist[2] = ’B’ mylist [1, 2, ’B’] l [1, 2, ’B’</p> <p>The exercise consists of defining a meta-class which implements the deep copy on the assignment operator and binding this to the standard class list. Such that the following behavior can be yielded</p> Print a List of Places (ie, Rankings) (Python) 2012-02-25T05:37:41-08:00Andrew Yurisichhttp://code.activestate.com/recipes/users/4180867/http://code.activestate.com/recipes/578052-print-a-list-of-places-ie-rankings/ <p style="color: grey"> Python recipe 578052 by <a href="/recipes/users/4180867/">Andrew Yurisich</a> (<a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/listing/">listing</a>, <a href="/recipes/tags/ranking/">ranking</a>). </p> <p>Hopefully this function will save you the trip to oocalc/excel. Py3k code.</p> Get multiple elements from a list (Python) 2011-11-21T06:41:57-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577953-get-multiple-elements-from-a-list/ <p style="color: grey"> Python recipe 577953 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/get/">get</a>, <a href="/recipes/tags/getvar/">getvar</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/retrieve/">retrieve</a>, <a href="/recipes/tags/retrieving/">retrieving</a>). </p> <p>This code lets you retrieve multiple elements from a list at one time</p> Random Binary List (Python) 2011-11-09T19:38:14-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577944-random-binary-list/ <p style="color: grey"> Python recipe 577944 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/base/">base</a>, <a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/random_binary_list/">random_binary_list</a>). </p> <p>This recipe returns a list of size n such that the contents of the returned list are random 0s and 1s. It returns a random binary list of size n.</p> Python Multidimensional List Searcher (Python) 2011-10-29T22:21:39-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577929-python-multidimensional-list-searcher/ <p style="color: grey"> Python recipe 577929 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/element/">element</a>, <a href="/recipes/tags/elements/">elements</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/finder/">finder</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>). </p> <p>This module/function lets you find a 2 dimensional list of indices for elements you are looking for in a super list. Example:</p> <p>find([1,1,1,2,1,2,3,3],[1,2,3])</p> <p>returns: [[0, 1, 2, 4], [3, 5], [6, 7]]</p> Simple recursive function to non-recursive function (Python) 2011-05-27T01:47:37-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577724-simple-recursive-function-to-non-recursive-functio/ <p style="color: grey"> Python recipe 577724 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/substitute/">substitute</a>). </p> <p>This recipe is a simple solution for turning a recursive function into a non-recursive function.</p> Get a list from a ConfigParser option (Python) 2011-05-13T13:53:56-07:00Georges Martinhttp://code.activestate.com/recipes/users/4177973/http://code.activestate.com/recipes/577694-get-a-list-from-a-configparser-option/ <p style="color: grey"> Python recipe 577694 by <a href="/recipes/users/4177973/">Georges Martin</a> (<a href="/recipes/tags/configparser/">configparser</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/option/">option</a>). Revision 2. </p> <p>Return a list from a ConfigParser option. By default, split on a comma and strip whitespaces.</p> Sublists (Python) 2010-12-21T16:28:20-08:00Michael Pucketthttp://code.activestate.com/recipes/users/4176295/http://code.activestate.com/recipes/577510-sublists/ <p style="color: grey"> Python recipe 577510 by <a href="/recipes/users/4176295/">Michael Puckett</a> (<a href="/recipes/tags/grouping/">grouping</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/sublist/">sublist</a>). </p> <p>The opposite of list flattening. </p> <p>Given a list and a length n return a list of sub lists of length n.</p>