Algorithm learning diary day05

I also learned the usage of python class yesterday 1. Def...

I also learned the usage of python class yesterday

1. Definition of class

Classes are generally defined by class. It's common practice to capitalize with the first letter. I inherited the object class in Python 3. I found the answer on how to inherit the object class

The object class inherits from the new class, and the object class does not inherit from the classic class. In Python 2.7, there are differences in multiple inheritance between the new class and the classic class:

class A: def foo(self): print('called A.foo()') class B(A): pass class C(A): def foo(self): print('called C.foo()') class D(B, C): pass if __name__ == '__main__': d = D() d.foo()

B. C is a subclass of A. D inherits more than two classes B and C. C overrides the foo() method in a.

If A is A classic class (the code above), when calling the foo() method of the instance of D, Python will search foo() according to the depth first method, the path is B-A-C, and the foo() in A is executed;

If A is A new class, when calling the foo() method of the instance of D, Python will search foo() according to the breadth first method, the path is B-C-A, and the foo() in C is executed.

Because D directly inherits from C, logically speaking, it is more reasonable to execute foo() in C, so the processing of multi inheritance by new classes is more logical.

The new classes in Python 3.x seem to be compatible with the classic classes. No matter whether A inherits the object class or not, foo() in D instance will execute foo() in C. However, this difference still exists in Python 2.7, so it is recommended to use A new class and inherit the object class.

A class is a template, and an instance is an object created from a class. The syntax of how to create classes and instances is as follows. For example, we create a Bike class and instances called giant and canyon, both of which are bicycles.

class Bike(object): #Create a bike class. Bike is the class name pass #It is used for table space occupation in empty functions and cannot be defaulted giant=Bike() #Create an instance named giant canyon=Bike() #Create an instance named canyon

2. Instance attribute and class attribute

Instance attributes are used to distinguish different instances; Class properties are common to each instance. For example, every bicycle (such as giant and canyon above) has wheels, but their painting is different. Here, the instance attribute is painting, and the class attribute is wheels. In the above, we created the Bike class, so our next step is to initialize it. We add one for the Bike class_ init_ (), when creating an instance_ init_ The () method is automatically called to add instance properties to the created instance.

class Bike(object): #Create a bike class. Bike is the class name def __init__(self, color): # Initialize a property color self.color = color # Indicates that we will assign a value to the instance property color we will create

Note:_ init_ The first parameter in (self, color) must be self. Self represents an instance of a class. It is an instance created through a class (we have not created this instance when defining a class, but it represents the instance we created when using a class). You can use another name, but it is recommended to use the custom self. Subsequent parameters can be specified freely, which is no different from defining a function

Class properties are shared by all instances. If you bind properties on a class, all instances can access the properties of the class, and the class properties accessed by all instances are the same. Wheel is a common attribute of bike. Each bike has two wheels:

class Bike(object): # Create a bike class. Bike is the class name wheel = 3.14 # There are two wheels, class attribute def __init__(self, color): # Initialize an instance property color self.color = color # Indicates that we will assign a value to the instance property color we will create giant=Bike(red) canyon=Bike(blue) print('----Before modification-----') print('wheel=\t', Bike.wheel) print('giant.wheel=\t', giant.wheel) # 2 print('canyon.wheel=\t', canyon.wheel) # 2 print('----After class name modification-----') Bike.wheel = 4 # The class properties of all instances are changed by modifying the class properties through the class name print('wheel=\t', Bike.wheel) print('giant.wheel=\t', giant.wheel) # 4 print('canyon.wheel=\t', canyon.wheel) # 4 print('----adopt circle1 After the instance name is modified-----') giant.wheel=6 # In fact, an instance attribute with the same name as the class attribute is created for giant print('wheel=\t', Bike.wheel) # 4 print('giant.wheel=\t', giant.wheel) # The access priority of instance property is higher than that of class property, so it is 6 print('canyon.wheel=\t', canyon.wheel) # 4

Note: class properties cannot be modified with instances!!! Class properties cannot be modified with instances!!! Class properties cannot be modified with instances!!!

Array: it's very simple. Just remember how to use it

import numpy as np A1=np.array([100,99,98]) #Create a one-dimensional array A2=np.array([[100,99,98],[97,96,95],[94,93,92]]) #Create 2D array A3=np.array([[[100,98,97],[96,95,94],[93,92,91]],[[90,89,88],[87,86,85]]]) #Create 3D array print('The third dimension in a three-dimensional array',A3[0]) print('2D in 3D array',A3[0][0]) print('The first dimension in a three-dimensional array',A3[0][0][0])

Tree: a tree refers to a data set of a tree structure with at least one Root node. There is a hierarchical relationship between nodes. The Root node of the tree structure is above, other nodes are below, and the bottom node is a leaf node. It looks like an upside down tree.

1. Characteristics of tree nodes

  • There is only one root node.
  • Each node has multiple or zero children.
  • The child node can have multiple child nodes or zero child nodes.
  • The lower node is a leaf node.
  • The previous node of any child node is called the parent node.
  • Several local nodes of the tree can form a subtree.
  • Level means that if the root node is level 1, its child nodes are level 2, and the child nodes of the child nodes are level 3, so as to push it to the bottom leaf layer. The maximum number of layers of a tree is the height or depth of the tree.

2. Tree classification

Tree roots can be divided into unordered trees and ordered trees according to whether there is order between child nodes.

  • Unordered tree (also known as free tree) means that there is no sequential relationship between the children of any node.

  • An ordered tree refers to an ordered relationship between the child nodes of any node.

    According to the maximum number of child nodes of a node, it can be divided into Binary Tree and non Binary Tree.

  • Binary tree refers to a data structure in which each node has at most two child nodes.

  • A non binary tree is a tree that is not a binary tree

3. Characteristics of binary tree

  • There are at most 2(i-1) nodes (I ≥ 1) on layer I. for example, the number of nodes in layer 2 is 22-1 = 2.
  • A binary tree with depth D has at most 2d-1 nodes (D ≥ 1). For example, the maximum node of a binary tree with depth d=3 is 23-1 = 7.

9 November 2021, 14:16 | Views: 9703

Add new comment

For adding a comment, please log in
or create account

0 comments