Page 208 -
P. 208

python toolbox



                       Your Python Toolbox


                       You’ve got Chapter 5 under your
                       belt and you’ve added some more
     CHAPTER 5
                       Python techiques to your toolbox.





                    Python Lingo                                        ƒ  The sort() method changes the

                    • “In-place” sorting - transforms                   ordering of lists in-place.

                     and then replaces.
                                                                        The sorted() BIF sorts most any data
                     • “Copied” sorting - transforms                    ƒ  structure by providing copied sorting.
                      and then returns.                                 ƒ  Pass reverse=True to either
                      • “Method Chaining” - reading                     ƒ  data in descending order.
                                                                        sort() or sorted() to arrange your
                       from left to right, applies a
                       collection of methods to data.
                                                                        When you have code like this:
                        • “Function Chaining” - reading                      new_l = []
                                                                            for t in old_l:
                        from right to left, applies a

                                                                                new_l.
                         collection of functions to data.
                                                                        append(len(t))
                                                                        rewrite it to use a list comprehension,
                                                                        like this:
                                                                            new_l = [len(t) for t
                             More Python Lingo                          in old_l]
                                                                        ƒ  To access more than one data item from
                             • “List Comprehension” - specify
                                                                        a list, use a slice. For example:
                             a transformation on one line (as
                                                                            my_list[3:6]
                            opposed to using an iteration).
                                                                        accesses the items from index location 3
                                                                        up-to-but-not-including index location 6.
                            • A “slice” - access more than one
                            item from a list.
                                                                        ƒ  Create a set using the set() factory
                                                                        function.
                           • A “set” - a collection of
                           unordered data items that
                           contains no duplicates.










           172    Chapter 5
   203   204   205   206   207   208   209   210   211   212   213