标签:role san expr keep data ssi power cep 可变
from fluent python:
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,需要注意
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的不同
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吗?)
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
. 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.
标签:role san expr keep data ssi power cep 可变
原文地址:https://www.cnblogs.com/cometous/p/10421505.html