Top-rated recipes tagged "tuple"http://code.activestate.com/recipes/tags/tuple/top/2017-01-17T20:05:10-08:00ActiveState Code RecipesClassifying letters as vowels or consonants and counting their frequencies (Python) 2017-01-17T20:05:10-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580749-classifying-letters-as-vowels-or-consonants-and-co/ <p style="color: grey"> Python recipe 580749 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/assertions/">assertions</a>, <a href="/recipes/tags/comprehension/">comprehension</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionaries/">dictionaries</a>, <a href="/recipes/tags/dict_comp/">dict_comp</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/tuple/">tuple</a>, <a href="/recipes/tags/unpack/">unpack</a>). </p> <p>This recipe shows how to take a string as input and classify the characters in it as vowels, consonants or neither. The frequency of each vowel is calculated and the frequency of all the consonants in total is calculated. The program logic is fairly simple, and uses a dictionary comprehension and a dict; the more interesting thing about it, is that it illustrates 8 Python language features in under 35 lines of code.</p> <p>More details and sample output here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/classifying-letters-and-counting-their.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/classifying-letters-and-counting-their.html</a></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> Flatten Array/Tuple (Python) 2011-10-31T10:53:39-07:00Luca Zarottihttp://code.activestate.com/recipes/users/4179728/http://code.activestate.com/recipes/577932-flatten-arraytuple/ <p style="color: grey"> Python recipe 577932 by <a href="/recipes/users/4179728/">Luca Zarotti</a> (<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/tuple/">tuple</a>). </p> <p>Flatten a nested array/tuple</p> Fast flatten() with depth control and oversight over which subtrees to expand (Python) 2010-11-26T11:10:01-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577470-fast-flatten-with-depth-control-and-oversight-over/ <p style="color: grey"> Python recipe 577470 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/optimal_solution/">optimal_solution</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/tuple/">tuple</a>). </p> <p>Extremely fast, non-recursive, depth limited flatten with powerful control over which subtrees are to be expanded. If this is what you need then look no further.</p> Clean preceeding and trailing whitespace in complex list dictionary tuple structures (Python) 2008-08-06T15:16:54-07:00Willhttp://code.activestate.com/recipes/users/4166209/http://code.activestate.com/recipes/576409-clean-preceeding-and-trailing-whitespace-in-comple/ <p style="color: grey"> Python recipe 576409 by <a href="/recipes/users/4166209/">Will</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/tuple/">tuple</a>, <a href="/recipes/tags/whitespace/">whitespace</a>). Revision 2. </p> <p>Function to clean trailing and or preceeding whitespace from string types in complex list, dictionary, and tuple structures. This is a recursive function to allow for complete coverage of all items in the structure. Wanted to share it as I needed it and after searching for a while I gave up and wrote one.</p> <p>For example a = ["\rText \r\n", "This one is fine", ["stuff ", [" Something Else"], 4, "Another ", "one", " with"], "\twhitespace\r\n"]</p> <p>print cleanWhiteSpace(a) Result: ["Text", "This one is fine", ["stuff", ["Something Else"], 4, "Another", "one", "with"], "whitespace"]</p>