Statistics
4
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-05-27

AuthorChristian Mayer

Python One-Liners will show readers how to perform useful tasks with one line of Python code. Following a brief Python refresher, the book covers essential advanced topics like slicing, regular expressions, list comprehension, broadcasting, lambda functions, algorithms, logistic regression and more. Each chapter introduces a problem to solve, walks the reader through the skills necessary to solve the problem, then provides a concise one-liner Python solution with a detailed explanation.

Tags
No tags
ISBN: 1718500505
Publisher: No Starch Press
Publish Year: 2019
Language: 英文
Pages: 216
File Format: PDF
File Size: 6.7 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Contents in Detail 1. Cover Page 2. Title Page 3. Copyright Page 4. Dedication 5. About the Author 6. About the Technical Reviewer 7. Brief Contents 8. Contents in Detail 9. Acknowledgments 10. Introduction 1. Python One-Liner Example 2. A Note on Readability 3. Who Is This Book For? 4. What Will You Learn? 5. Online Resources 11. 1 Python Refresher 1. Basic Data Structures 2. Container Data Structures 3. Control Flow 4. Functions 5. Lambdas 6. Summary 12. 2 Python Tricks 1. Using List Comprehension to Find Top Earners 2. Using List Comprehension to Find Words with High Information Value 3. Reading a File 4. Using Lambda and Map Functions 5. Using Slicing to Extract Matching Substring Environments 6. Combining List Comprehension and Slicing 7. Using Slice Assignment to Correct Corrupted Lists 8. Analyzing Cardiac Health Data with List Concatenation 9. Using Generator Expressions to Find Companies That Pay Below Minimum Wage 10. Formatting Databases with the zip() Function 11. Summary 13. 3 Data Science 1. Basic Two-Dimensional Array Arithmetic
2. Working with NumPy Arrays: Slicing, Broadcasting, and Array Types 3. Conditional Array Search, Filtering, and Broadcasting to Detect Outliers 4. Boolean Indexing to Filter Two-Dimensional Arrays 5. Broadcasting, Slice Assignment, and Reshaping to Clean Every i-th Array Element 6. When to Use the sort() Function and When to Use the argsort() Function in NumPy 7. How to Use Lambda Functions and Boolean Indexing to Filter Arrays 8. How to Create Advanced Array Filters with Statistics, Math, and Logic 9. Simple Association Analysis: People Who Bought X Also Bought Y 10. Intermediate Association Analysis to Find Bestseller Bundles 11. Summary 14. 4 Machine Learning 1. The Basics of Supervised Machine Learning 2. Linear Regression 3. Logistic Regression in One Line 4. K-Means Clustering in One Line 5. K-Nearest Neighbors in One Line 6. Neural Network Analysis in One Line 7. Decision-Tree Learning in One Line 8. Get Row with Minimal Variance in One Line 9. Basic Statistics in One Line 10. Classification with Support-Vector Machines in One Line 11. Classification with Random Forests in One Line 12. Summary 15. 5 Regular Expressions 1. Finding Basic Textual Patterns in Strings 2. Writing Your First Web Scraper with Regular Expressions 3. Analyzing Hyperlinks of HTML Documents 4. Extracting Dollars from a String 5. Finding Nonsecure HTTP URLs 6. Validating the Time Format of User Input, Part 1 7. Validating Time Format of User Input, Part 2 8. Duplicate Detection in Strings 9. Detecting Word Repetitions 10. Modifying Regex Patterns in a Multiline String 11. Summary 16. 6 Algorithms 1. Finding Anagrams with Lambda Functions and Sorting
2. Finding Palindromes with Lambda Functions and Negative Slicing 3. Counting Permutations with Recursive Factorial Functions 4. Finding the Levenshtein Distance 5. Calculating the Powerset by Using Functional Programming 6. Caesar’s Cipher Encryption Using Advanced Indexing and List Comprehension 7. Finding Prime Numbers with the Sieve of Eratosthenes 8. Calculating the Fibonacci Series with the reduce() Function 9. A Recursive Binary Search Algorithm 10. A Recursive Quicksort Algorithm 11. Summary 17. Afterword 18. Index 1. i 2. ii 3. iii 4. iv 5. v 6. vi 7. vii 8. viii 9. ix 10. x 11. xi 12. xii 13. xiii 14. xiv 15. xv 16. xvi 17. xvii 18. xviii 19. xix 20. xx 21. xxi 22. xxii 23. xxiii 24. xxiv 25. 1 26. 2 27. 3 28. 4 29. 5 30. 6 31. 7 32. 8 33. 9 34. 10
35. 11 36. 12 37. 13 38. 14 39. 15 40. 16 41. 17 42. 18 43. 19 44. 20 45. 21 46. 22 47. 23 48. 24 49. 25 50. 26 51. 27 52. 28 53. 29 54. 30 55. 31 56. 32 57. 33 58. 34 59. 35 60. 36 61. 37 62. 38 63. 39 64. 40 65. 41 66. 42 67. 43 68. 44 69. 45 70. 46 71. 47 72. 48 73. 49 74. 50 75. 51 76. 52 77. 53 78. 54 79. 55 80. 56 81. 57 82. 58 83. 59 84. 60 85. 61 86. 62 87. 63 88. 64
89. 65 90. 66 91. 67 92. 68 93. 69 94. 70 95. 71 96. 72 97. 73 98. 74 99. 75 100. 76 101. 77 102. 78 103. 79 104. 80 105. 81 106. 82 107. 83 108. 84 109. 85 110. 86 111. 87 112. 88 113. 89 114. 90 115. 91 116. 92 117. 93 118. 94 119. 95 120. 96 121. 97 122. 98 123. 99 124. 100 125. 101 126. 102 127. 103 128. 104 129. 105 130. 106 131. 107 132. 108 133. 109 134. 110 135. 111 136. 112 137. 113 138. 114 139. 115 140. 116 141. 117 142. 118
143. 119 144. 120 145. 121 146. 122 147. 123 148. 124 149. 125 150. 126 151. 127 152. 128 153. 129 154. 130 155. 131 156. 132 157. 133 158. 134 159. 135 160. 136 161. 137 162. 138 163. 139 164. 140 165. 141 166. 142 167. 143 168. 144 169. 145 170. 146 171. 147 172. 148 173. 149 174. 150 175. 151 176. 152 177. 153 178. 154 179. 155 180. 156 181. 157 182. 158 183. 159 184. 160 185. 161 186. 162 187. 163 188. 164 189. 165 190. 166 191. 167 192. 168 193. 169 194. 170 195. 171 196. 172
197. 173 198. 174 199. 175 200. 176 201. 177 202. 178 203. 179 204. 180 205. 181 206. 182 207. 183 208. 184 209. 185 210. 186 211. 187 212. 188 213. 189 214. 190 215. 191 216. BM-1
PYTHON ONE-LINERS Write Concise, Eloquent Python Like a Professional by Christian Mayer San Francisco
PYTHON ONE-LINERS. Copyright © 2020 by Christian Mayer. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-10: 1-7185-0050-5 ISBN-13: 978-1-7185-0050-1 Publisher: William Pollock Production Editors: Janelle Ludowise and Kassie Andreadis Cover Illustration: Rob Gale Interior Design: Octopod Studios Developmental Editors: Liz Chadwick and Alex Freed Technical Reviewer: Daniel Zingaro Copyeditor: Sharon Wilkey Compositor: Danielle Foster Proofreader: James Fraleigh Indexer: JoAnne Burek For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1.415.863.9900; info@nostarch.com www.nostarch.com The Library of Congress issued the following Cataloging-in-Publication Data for the first edition: Names: Mayer, Christian (Computer Scientist), author. Title: Python one-liners: write concise, eloquent Python like a professional / Christian Mayer. Description: San Francisco : No Starch Press, Inc., 2020. | Includes index. Identifiers: LCCN 2020001449 (print) | LCCN 2020001450 (ebook) | ISBN 9781718500501 | ISBN 9781718500518 (ebook) Subjects: LCSH: Python (Computer program language) Classification: LCC QA76.73.P98 M39 2020 (print) | LCC QA76.73.P98 (ebook) | DDC 005.13/3--dc23 LC record available at https://lccn.loc.gov/2020001449 LC ebook record available at https://lccn.loc.gov/2020001450 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
To my wife Anna
About the Author Christian Mayer is a doctor of computer science and the founder and maintainer of the popular Python site https://blog.finxter.com/ and its associated newsletter, which has 20,000 active subscribers and is still growing. His rapidly growing websites help tens of thousands of students improve their coding skills and online businesses. Christian is also the author of the Coffee Break Python series of self-published books.
About the Technical Reviewer Dr. Daniel Zingaro is an assistant teaching professor of computer science and award-winning teacher at the University of Toronto. His main area of research is computer science education, where he studies how students learn (and sometimes don’t learn) computer science material. He is the author of Algorithmic Thinking (forthcoming from No Starch Press).
BRIEF CONTENTS Acknowledgments Introduction Chapter 1: Python Refresher Chapter 2: Python Tricks Chapter 3: Data Science Chapter 4: Machine Learning Chapter 5: Regular Expressions Chapter 6: Algorithms Afterword Index
CONTENTS IN DETAIL ACKNOWLEDGMENTS INTRODUCTION Python One-Liner Example A Note on Readability Who Is This Book For? What Will You Learn? Online Resources 1 PYTHON REFRESHER Basic Data Structures Numerical Data Types and Structures Booleans Strings The Keyword None Container Data Structures Lists Stacks Sets Dictionaries Membership List and Set Comprehension Control Flow if, else, and elif Loops Functions Lambdas Summary 2 PYTHON TRICKS
Using List Comprehension to Find Top Earners The Basics The Code How It Works Using List Comprehension to Find Words with High Information Value The Basics The Code How It Works Reading a File The Basics The Code How It Works Using Lambda and Map Functions The Basics The Code How It Works Using Slicing to Extract Matching Substring Environments The Basics The Code How It Works Combining List Comprehension and Slicing The Basics The Code How It Works Using Slice Assignment to Correct Corrupted Lists The Basics The Code How It Works Analyzing Cardiac Health Data with List Concatenation The Basics The Code How It Works
Using Generator Expressions to Find Companies That Pay Below Minimum Wage The Basics The Code How It Works Formatting Databases with the zip() Function The Basics The Code How It Works Summary 3 DATA SCIENCE Basic Two-Dimensional Array Arithmetic The Basics The Code How It Works Working with NumPy Arrays: Slicing, Broadcasting, and Array Types The Basics The Code How It Works Conditional Array Search, Filtering, and Broadcasting to Detect Outliers The Basics The Code How It Works Boolean Indexing to Filter Two-Dimensional Arrays The Basics The Code How It Works Broadcasting, Slice Assignment, and Reshaping to Clean Every i-th Array Element The Basics The Code
How It Works When to Use the sort() Function and When to Use the argsort() Function in NumPy The Basics The Code How It Works How to Use Lambda Functions and Boolean Indexing to Filter Arrays The Basics The Code How It Works How to Create Advanced Array Filters with Statistics, Math, and Logic The Basics The Code How It Works Simple Association Analysis: People Who Bought X Also Bought Y The Basics The Code How It Works Intermediate Association Analysis to Find Bestseller Bundles The Basics The Code How It Works Summary 4 MACHINE LEARNING The Basics of Supervised Machine Learning Training Phase Inference Phase Linear Regression The Basics The Code
How It Works Logistic Regression in One Line The Basics The Code How It Works K-Means Clustering in One Line The Basics The Code How It Works K-Nearest Neighbors in One Line The Basics The Code How It Works Neural Network Analysis in One Line The Basics The Code How It Works Decision-Tree Learning in One Line The Basics The Code How It Works Get Row with Minimal Variance in One Line The Basics The Code How It Works Basic Statistics in One Line The Basics The Code How It Works Classification with Support-Vector Machines in One Line The Basics The Code How It Works Classification with Random Forests in One Line