1. tuple (tuple)
tuple
and list
very similar, but the tuple cannot be modified once initialized, as in:
classmates = ('Michael', 'Bob', 'Tracy')
classmates
This tuple
cannot be changed, nor does it have append(),insert()
such a method. Other methods of getting elements list
are 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 tuple
of immutability, the code is safer. If possible, use it tuple
insteadlist
tuple
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 tuple
and parentheses in mathematical formulas, which creates ambiguity. Therefore, it is Python
stipulated 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 tuple
to disambiguate:
t = (1,)
t
(1,)
Python
When 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_index
toend_index-1
③ con[start_index: end_index : step]
: Returns a continuous object whose index value is start_index
between end_index-1
and the difference between the index value start_index
and the difference can be step
divisible, that is, the step size
2. Default usage
① con[start_index: ]
: Default end_index
, from the start_index
beginning to the last object in the sequence
②Default , con[: end_index]:
indicating the fragment start_index
from the first object in the sequence to theend_index-1
③ con[:]
: Default start_index
sum end_index
, representing the complete fragment from the first object to the last object
④ con[::step]
: Default start_index
sum end_index
, indicating that the entire sequence is step
valued 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
s[::-1]
Can be seen as a flip effect
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:
x[:,1] takes all the values in the x-axis direction, and the y-axis is equal to 1, the result:
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:
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)