Python study notes organization (python 3)

1. tuple (tuple)

tupleand listvery similar, but the tuple cannot be modified once initialized, as in:

classmates = ('Michael', 'Bob', 'Tracy')

classmatesThis tuplecannot be changed, nor does it have append(),insert()such a method. Other methods of getting elements listare the same, you can use them normally classmates[0],classmates[-1], but you cannot assign them to other elements

What's the point of immutable tuple? Because tupleof immutability, the code is safer. If possible, use it tupleinsteadlisttuple

The tuple's trap

To define one with only 1 element tuple, if you define it like this:

t = (1)
t
1

The definition is not tuple, it is the number 1! This is because parentheses ()can represent both parentheses tupleand parentheses in mathematical formulas, which creates ambiguity. Therefore, it is Pythonstipulated that in this case, the calculation result is naturally 1 when the parentheses are used for calculation.
Therefore, a comma, , must be added to the definition of only 1 element tupleto disambiguate:

t = (1,)
t
(1,)

PythonWhen displaying only one element tuple, a comma, , will also be added, so as not to be misunderstood as parentheses in the sense of mathematical calculation.

Finally look at a "mutable" tuple:

t = ('a', 'b', ['A', 'B'])
t[2][0] = 'X'
t[2][1] = 'Y'
t
('a', 'b', ['X', 'Y'])

Second, the list type slice in Python

1. Basic usage

① : Returns the object con[start_index]whose index value isstart_index

② : Returns a continuous object whose con[start_index: end_index]index value is between start_indextoend_index-1

con[start_index: end_index : step]: Returns a continuous object whose index value is start_indexbetween end_index-1and the difference between the index value start_indexand the difference can be stepdivisible, that is, the step size

write picture description here

2. Default usage

con[start_index: ]: Default end_index, from the start_indexbeginning to the last object in the sequence

②Default , con[: end_index]:indicating the fragment start_indexfrom the first object in the sequence to theend_index-1

con[:]: Default start_indexsum end_index, representing the complete fragment from the first object to the last object

con[::step]: Default start_indexsum end_index, indicating that the entire sequence is stepvalued according to the rule that the index can be divisible

Negative numbers can be used for slicing. Negative numbers are used at positions counted from the end of the sequence

write picture description here

s[::-1]Can be seen as a flip effect

write picture description here

3. Two-dimensional ndarray slice in Numpy

The specific usage for one-dimensional data is similar to that in Python

For two-dimensional values, the usage is as follows. X[:,:] separates two dimensions with commas. The usage is the same for each dimension. Take the following two-dimensional array as an example:

write picture description here

write picture description here

x[:,1] takes all the values ​​in the x-axis direction, and the y-axis is equal to 1, the result:

write picture description here

x[1:3,1] Take the x-axis x is greater than or equal to 1, less than 3, and the y-axis is equal to 1, the result:

write picture description here

3. Simple usage of lambda expressions

For simple functions, there is also a convenient way of expressing them, namely: lambda expressions

# ###################### 普通函数 ######################
# 定义函数(普通方式)
def func(arg):
    return arg + 1

# 执行函数
result = func(123)

# ###################### lambda ######################

# 定义函数(lambda表达式)
my_lambda = lambda arg : arg + 1

# 执行函数
result = my_lambda(123)

Four,__call__

class X(object):
    def __init__(self, a, b, range):
        self.a = a
        self.b = b
        self.range = range
    def __call__(self, a, b):
        self.a = a
        self.b = b
        print('__call__ with ({}, {})'.format(self.a, self.b))
    def __del__(self, a, b, range):
        del self.a
        del self.b
        del self.range
>>> xInstance = X(1, 2, 3)
>>> xInstance(1,2)
__call__ with (7, 8)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326045625&siteId=291194637