1. Data structure 1.1 built-in sequence type
Four types of sequences:
1. Container sequence: list, tuple, and Collections.deque
2. Flat sequence: str, bytes, ByteArray, Memoryview and Array.array
3. Variable sequence: list, ByteArray, Array.array, Collections.deque, and Memoryview
4. Immutable sequences: tuple, str, and bytes
1.2 List derivation
1. List comprehension: List Derivation is a shortcut to the build list, a concise notation.
List derivation has only one effect: The list is generated.
' $¢£¥€¤ ' for inch symbols] # enclosed in square brackets, internally generates multiple values with a for loop >>> codes[36, 162, 163, 165, 8364, 164]
2. Variables and assignments within an expression only work locally, and variables with the same name in the context of an expression can be referenced normally, and local variables do not affect them.
' ABC ' for inch >>>'ABC' >>> dummy [65, 66, 67]
That is, the value of x is not affected by the X value in the loop in the list deduction.
1.3 Generator expression
1. Generative expression (generator expressions): The generator expression adheres to the iterator protocol and can produce elements individually instead of creating a complete list before passing the list to a constructor. This saves more memory.
The syntax of the generator expression is similar to the list deduction, except that the brackets are replaced with parentheses .
Initialize tuples and arrays with generator expressions:
>>> symbols ='$¢£¥€¤'# Initialize tuple >>> tuple (ord (symbol) forSymbolinchsymbols) #If the generator expression is the only parameter in a function call procedure, you do not need to surround it with extra parentheses.(36, 162, 163, 165, 8364, 164) >>>ImportArray # initializing arrays>>> Array.array ('I', (ord (symbol) forSymbolinchsymbols)) # The construction method of the array requires two parameters, so parentheses are required. The first parameter of the array constructor method, (' I ') specifies how the numbers in the array are stored. Array ('I', [36, 162, 163, 165, 8364, 164]
1.4 Cartesian product
A Cartesian product is a list of elements that are composed of elements of an input iteration type, so the length of the Cartesian product list is equal to the product of the length of the input variable.
1. Calculating Cartesian product using list derivation
>>> colors = ['Black',' White'] >>> sizes = ['S','M','L'] >>> tshirts = [(color, size) forColorinchColors forSizeinchSizes]>>> Tshirts
[('Black','S'), ('Black','M'), ('Black','L'), (' White','S'), (' White','M'), (' White','L')] >>> forColorinchcolors: # Use for loop is the same effect ... forSizeinchSizes: ...Print((color, size)) ... ('Black','S') ('Black','M') ('Black','L') (' White','S') (' White','M') (' White','L') >>> tshirts = [(color, size) forSizeinchSizes forColorinchcolors] # change parameter position, output order is different>>>Tshirts [('Black','S'), (' White','S'), ('Black','M'), (' White','M'), ('Black','L'), (' White','L')]
2. Calculating Cartesian product using generator expressions
>>> colors = ['Black',' White']>>> sizes = ['S','M','L']>>> forTshirtinch('{} {}'. format (c, s) forCinchColors forSinchsizes): ...Print(tshirt) ... black sblack mblack lwhite swhite mwhite L
Fluent Python and Cookbook learning Notes (i)