www.it-ebooks.info www.it-ebooks.info THIRD EDITION Python Cookbook David Beazley and Brian K. Jones www.it-ebooks.info Python Cookbook, Third Edition by David Beazley and Brian K. Jones Copyright © 2013 David Beazley and Brian Jones. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editors: Meghan Blanchette and Rachel Roumeliotis Production Editor: Kristen Borg Copyeditor: Jasmine Kwityn Proofreader: BIM Proofreading Services May 2013: Indexer: WordCo Indexing Services Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Third Edition Revision History for the Third Edition: 2013-05-08: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449340377 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Python Cookbook, the image of a springhaas, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-34037-7 [LSI] www.it-ebooks.info Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi 1. Data Structures and Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1. Unpacking a Sequence into Separate Variables 1.2. Unpacking Elements from Iterables of Arbitrary Length 1.3. Keeping the Last N Items 1.4. Finding the Largest or Smallest N Items 1.5. Implementing a Priority Queue 1.6. Mapping Keys to Multiple Values in a Dictionary 1.7. Keeping Dictionaries in Order 1.8. Calculating with Dictionaries 1.9. Finding Commonalities in Two Dictionaries 1.10. Removing Duplicates from a Sequence while Maintaining Order 1.11. Naming a Slice 1.12. Determining the Most Frequently Occurring Items in a Sequence 1.13. Sorting a List of Dictionaries by a Common Key 1.14. Sorting Objects Without Native Comparison Support 1.15. Grouping Records Together Based on a Field 1.16. Filtering Sequence Elements 1.17. Extracting a Subset of a Dictionary 1.18. Mapping Names to Sequence Elements 1.19. Transforming and Reducing Data at the Same Time 1.20. Combining Multiple Mappings into a Single Mapping 1 3 5 7 8 11 12 13 15 17 18 20 21 23 24 26 28 29 32 33 2. Strings and Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.1. Splitting Strings on Any of Multiple Delimiters 2.2. Matching Text at the Start or End of a String 2.3. Matching Strings Using Shell Wildcard Patterns 2.4. Matching and Searching for Text Patterns 37 38 40 42 iii www.it-ebooks.info 2.5. Searching and Replacing Text 2.6. Searching and Replacing Case-Insensitive Text 2.7. Specifying a Regular Expression for the Shortest Match 2.8. Writing a Regular Expression for Multiline Patterns 2.9. Normalizing Unicode Text to a Standard Representation 2.10. Working with Unicode Characters in Regular Expressions 2.11. Stripping Unwanted Characters from Strings 2.12. Sanitizing and Cleaning Up Text 2.13. Aligning Text Strings 2.14. Combining and Concatenating Strings 2.15. Interpolating Variables in Strings 2.16. Reformatting Text to a Fixed Number of Columns 2.17. Handling HTML and XML Entities in Text 2.18. Tokenizing Text 2.19. Writing a Simple Recursive Descent Parser 2.20. Performing Text Operations on Byte Strings 45 46 47 48 50 52 53 54 57 58 61 64 65 66 69 78 3. Numbers, Dates, and Times. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PythonCookbook3rd
温馨提示:如果当前文档出现乱码或未能正常浏览,请先下载原文档进行浏览。
本文档由 user 于 2020-12-27 08:20:26上传分享