【Fluent Python】 chaper2

from fluent python:

  • Python sequences are often categorized as mutable or immutable, but it is also useful to consider a different axis: flat sequences and container sequences. The former are more compact, faster, and easier to use, but are limited to storing atomic data such as numbers, characters, and bytes.

Container sequences are more flexible, but may surprise you when they hold mutable objects, so you need to be careful to use them correctly with nested data structures

 

Mutable sequences : list, bytearray, array.array, collections.deque, and memoryview

Immutable sequences :tuple, str, and bytes

不可改变: 不可assign,update或删除某个元素(?)

 

flat sequence : only limit to numbers ,chars and bytes .

contaniner sequentces; 灵活但是如果包含可变objects,需要注意

  • list comprehension and generator 用来创建和初始化序列很有用。

A quick way to build a sequence is using a list comprehension (if the target is a list) or a generator expression (for all other kinds of sequences). 

.List comprehensions and generator expressions are powerful notations to build and initialize sequences. If you are not yet comfortable with them, take the time to master their basic usage. It is not hard, and soon you will be hooked.

 

 

 例2-5显示了generator 的用法,注意和list comprehension的不同

  •  Tuples in Python play two roles: as records with unnamed fields and as immutable lists. When a tuple is used as a record, tuple unpacking is the safest, most readable way of getting at the fields. The new * syntax makes tuple unpacking even better by making it easier to ignore some fields and to deal with optional fields. Named tuples are not so new, but deserve more attention: like tuples, they have very little overhead per instance, yet provide convenient access to the fields by name and a handy ._asdict() to export the record as an OrderedDict.

 note: Unlike lists, tuples often hold items of different types. That is natural, considering that each item in a tuple is really a field, and each field type is independent of the others.

         tuple 作为record,存储着不同类型的element:

注意tuple的unpacking功能!(在function/method的参数前加*号也是一种unpacking吗?)

 

 

  • Namedtuple 非常有用,对于根据name去取得字段以及._asdict() to export the record as an OrderedDict.

 

  •  Sequence slicing : powerful!

  Sequence slicing is a favorite Python syntax feature, and it is even more powerful than many realize. Multidimensional slicing and ellipsis (...) notation, as used in NumPy, may also be supported by user-defined sequences. Assigning to slices is a very expressive way of editing mutable sequences

  • Augmented assignment with += and *= behaves differently for mutable and immutable sequences

 . Repeated concatenation as in seq * n is convenient and, with care, can be used to initialize lists of lists containing immutable items. Augmented assignment with += and *= behaves differently for mutable and immutable sequences. In the latter case, these operators necessarily build new sequences. But if the target sequence is mutable, it is usually changed in place—but not always, depending on how the sequence is implemented.

 

  • The sort method and the sorted built-in function are easy to use and flexible, thanks to the key optional argument they accept, with a function to calculate the ordering criterion. By the way, key can also be used with the min and max built-in functions. To keep a sorted sequence in order, always insert items into it using bisect.insort; to search it efficiently, use bisect.bisect.

 

  • Beyond lists and tuples, the Python standard library provides array.array. Although NumPy and SciPy are not part of the standard library, if you do any kind of numerical processing on large sets of data, studying even a small part of these libraries can take you a long way. We closed by visiting the versatile and thread-safe collections.deque, comparing its API with that of list in Table 2-3 and mentioning other queue implementations in the standard library.

 

猜你喜欢

转载自www.cnblogs.com/cometous/p/10421505.html