12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

12.10. Glossary 123>>> t3 = [1, 2, 3, 4.0, '5', '6', [7], [8], 9]>>> print structshape(t3)list of (3 int, float, 2 str, 2 list of int, int)Here’s alistoftuples:>>> s = 'abc'>>> lt = zip(t, s)>>> print structshape(lt)list of 3 tuple of (int, str)And here’s adictionary with3itemsthat map integers tostrings.>>> d = dict(lt)>>> print structshape(d)dict of 3 int->strIfyou arehaving trouble keeping track of your data structures,structshapecan help.12.10 Glossarytuple: Animmutable sequence of elements.tupleassignment: An assignment with a sequence on the right side and a tuple of variables on theleft. Therightsideisevaluatedandthenitselementsareassignedtothevariablesontheleft.gather: The operation of assembling avariable-length argument tuple.scatter: The operation oftreating asequence as alistof arguments.DSU: Abbreviation of “decorate-sort-undecorate,” a pattern that involves building a list of tuples,sorting, and extracting part of theresult.data structure: A collection of related values, oftenorganized inlists,dictionaries, tuples,etc.shape (of adata structure): A summary of thetype, sizeand composition ofadata structure.12.11 ExercisesExercise 12.3 Write a function called most_frequent that takes a string and prints the letters indecreasingorderoffrequency. Findtextsamplesfromseveraldifferentlanguagesandseehowletterfrequency varies between languages. Compare your results with the tables at wikipedia.org/wiki/Letter_frequencies.Exercise 12.4 Moreanagrams!1. Write a program that reads a word list from a file (see Section 9.1) and prints all the sets ofwords that areanagrams.Here isan example of what theoutput might looklike:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!