Popular recipes tagged "list" but not "python"http://code.activestate.com/recipes/tags/list-python/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> 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> 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> Evolutionary Algorithm (Generation of Prime Numbers) (Python) 2011-11-27T06:45:00-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577964-evolutionary-algorithm-generation-of-prime-numbers/ <p style="color: grey"> Python recipe 577964 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/genetic/">genetic</a>, <a href="/recipes/tags/genetic_algorithm/">genetic_algorithm</a>, <a href="/recipes/tags/genetic_algorithms/">genetic_algorithms</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/of/">of</a>, <a href="/recipes/tags/prime/">prime</a>, <a href="/recipes/tags/primelist/">primelist</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/theory/">theory</a>). </p> <p>This is an evolutionary algorithm that returns a random list of prime numbers. This code is highly inefficient for a reason. This algorithm is more of a proof of concept that if a prime was a heritable trait, it would not be a desired one. </p> <p>Parameters:</p> <p>isPrime --> n: number to check if it is prime allPrimes --> n: size of list of random primes, m: the primes in the list will be between 0 and m</p> primeList (Python) 2011-11-05T23:42:37-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577935-primelist/ <p style="color: grey"> Python recipe 577935 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/prime/">prime</a>, <a href="/recipes/tags/primelist/">primelist</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/theory/">theory</a>). Revision 3. </p> <p>This module returns all the prime numbers strictly less than n. For this code to print out all of the primes n inclusive, in the range, n+1 must be substituted for n.</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> GCD of an arbitrary list (Python) 2010-12-22T00:05:45-08:00Jason Schornhttp://code.activestate.com/recipes/users/4176312/http://code.activestate.com/recipes/577512-gcd-of-an-arbitrary-list/ <p style="color: grey"> Python recipe 577512 by <a href="/recipes/users/4176312/">Jason Schorn</a> (<a href="/recipes/tags/arbitrary/">arbitrary</a>, <a href="/recipes/tags/common/">common</a>, <a href="/recipes/tags/divisor/">divisor</a>, <a href="/recipes/tags/gcd/">gcd</a>, <a href="/recipes/tags/greatest/">greatest</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/schorn/">schorn</a>). </p> <p>Given an arbitrary list (of length &gt;= 1) of positive integers, return the greatest common divisor (<code>gcd</code>) of the list.</p> Flattening lists (Python) 2010-12-21T16:04:38-08:00Michael Pucketthttp://code.activestate.com/recipes/users/4176295/http://code.activestate.com/recipes/577509-flattening-lists/ <p style="color: grey"> Python recipe 577509 by <a href="/recipes/users/4176295/">Michael Puckett</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>). </p> <p>Classic / frequently asked, <em>'How to flatten a list, regardless the nesting.'</em></p> Generate List of Numbers from Hyphenated and comma separeted string like "1-5,25-30,4,5" (Python) 2010-07-01T02:08:09-07:00Siddhant Sanyamhttp://code.activestate.com/recipes/users/4174317/http://code.activestate.com/recipes/577279-generate-list-of-numbers-from-hyphenated-and-comma/ <p style="color: grey"> Python recipe 577279 by <a href="/recipes/users/4174317/">Siddhant Sanyam</a> (<a href="/recipes/tags/hypen/">hypen</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/range/">range</a>). </p> <p>This function takes a range in form of "a-b" and generate a list of numbers between a and b inclusive. Also accepts comma separated ranges like "a-b,c-d,f" will build a list which will include numbers from a to b, a to d and f Example: hyphen_range('54-78') hyphen_range('57-78,454,45,1-10') hyphen_range('94-100,1052,2-50')</p> Python 2.7 Linked List vs List (Python) 2010-08-13T04:05:42-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577355-python-27-linked-list-vs-list/ <p style="color: grey"> Python recipe 577355 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/list/">list</a>). </p> <p>Linked list implementation based on Python 2.7 collections.MutableSequence with a few benchmarks comparing the linked list with the built-in list type.</p>