Fluent python 笔记及代码(2)序列

第二章 序列

python的序列可以根据可变和不可变分为 可变序列 不可变序列
可变序列:
list、bytearray、array.array、collections.deque 和 memoryview等
不可变序列:
tuple、str 和 bytes等

还可以根据可存放元素的类型是否一致分为扁平序列和容器序列

容器序列:

可以存放不同数据类型的序列,且存放的是对像的引用
list、tuple 和 collections.deque

扁平序列 :

只能存放一种类型的数据且存放的是值而不是引用,扁平序列其实是一段连续的内存空间。
str、bytes、bytearray、memoryview 和 array.array

列表推导和生成器表达式

生成器表达式()会逐个产生元素而不是和列表推导一样一次性生成,因此在一些情况下它更省内存

拆包

可以应用于所有可迭代对象
可以用*来拆分可迭代对象,并将其作为参数传入函数,也可以接受返回值

切片操作

切片操作实际上调用的是__ getitem __(slice(start,stop,step))
slice是python的一个切片对象

增量赋值

如+=的背后是__ iadd __()如果对象不可变才会调用 __ add __ ()
如果在元组中存在可变对象
如t=(1,2,[3,4])
执行t[2]+=[5,6]时 会报错但t也会被成功修改
因此最好在元组中不要存放可变对象,另外可以看出增量赋值不是原子性的操作

排序

list.sort 方法原地修改
sorted 方法产生新的列表,不管传入的是什么类型的序列
它们有共同的关键字:reserve 默认False 升序
key 一个只有一个参数的函数,根据函数的返回值进行排序

bisect模块

包含两个主要函数 bisect和insort 都是使用二分法在有序序列中查找和插入元素

数组

如果是对数字的处理,数组会更加高效,因为数组存储的是数字的机器翻译

memoryview

memoryview是一个内置类,它能让用户在不复制内容的情况下操作同一个数组的不同切片。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python’s simplicity lets you become productive quickly, but this often means you aren’t using everything it has to offer. With this hands-on guide, you’ll learn how to write effective, idiomatic Python code by leveraging its best—and possibly most neglected—features. Author Luciano Ramalho takes you through Python’s core language features and libraries, and shows you how to make your code shorter, faster, and more readable at the same time. Many experienced programmers try to bend Python to fit patterns they learned from other languages, and never discover Python features outside of their experience. With this book, those Python programmers will thoroughly learn how to become proficient in Python 3. This book covers: Python data model: understand how special methods are the key to the consistent behavior of objects Data structures: take full advantage of built-in types, and understand the text vs bytes duality in the Unicode age Functions as objects: view Python functions as first-class objects, and understand how this affects popular design patterns Object-oriented idioms: build classes by learning about references, mutability, interfaces, operator overloading, and multiple inheritance Control flow: leverage context managers, generators, coroutines, and concurrency with the concurrent.futures and asyncio packages Metaprogramming: understand how properties, attribute descriptors, class decorators, and metaclasses work Table of Contents Part I. Prologue Chapter 1. The Python Data Model Part II. Data structures Chapter 2. An array of sequences Chapter 3. Dictionaries and sets Chapter 4. Text versus bytes Part III. Functions as objects Chapter 5. First-class functions Chapter 6. Design patterns with first-class functions Chapter 7. Function decorators and closures Part IV. Object Oriented Idioms Chapter 8. Object references, mutability and recycling Chapter 9. A Pythonic object Chapter 10. Sequence hacking, hashing and slicing Chapter 11. Interfaces: from protocols to ABCs Chapter 12. Inheritance: for good or for worse Chapter 13. Operator overloading: doing it right Part V. Control flow Chapter 14. Iterables, iterators and generators Chapter 15. Context managers and else blocks Chapter 16. Coroutines Chapter 17. Concurrency with futures Chapter 18. Concurrency with asyncio Part VI. Metaprogramming Chapter 19. Dynamic attributes and properties Chapter 20. Attribute descriptors Chapter 21. Class metaprogramming Appendix A. Support scripts
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值