Python knowledge points

Python syntax

# The shallow copy operation will only copy the first level of the object being copied, and for the deeper level, it just copies its reference, as in the following example`a[2]`
# with`lst[2]`These two objects are the second layer. In fact, after the shallow copy, these two objects are still one object. Deep copy will be copied completely
# All hierarchical objects of the shell object are a true copy.
>>> from copy import copy, deepcopy
>>> lst =[1,2,[3,4]]>>> a, b =copy(lst),deepcopy(lst)>>> a,b([1,2,[3,4]],[1,2,[3,4]])>>>id(lst[2]),id(a[2]),id(b[2])(139842737414224,139842737414224,139842737414584)>>> lst[0]=10>>> a
[1,2,[3,4]]>>> b
[1,2,[3,4]]>>> lst[2][0]='test'>>> lst
[10,2,[' test',4]]>>> a
[1,2,[' test',4]]>>> b
[1,2,[3,4]]
>>> def foo(a, b=[1,2]):print(b)
  b.append(a)print(b)>>> val =4>>>foo(val)
# [1,2]
# [1,2,4]>>> foo(val)
# [1,2,4]
# [1,2,4,4]
# Here you can see that the second time the function is executed, the value of the default parameter b has become`[1, 2, 4]`The reason is that the default parameter is only in the first
# It will be initialized when executed once, and will be used by default later**This object after initialization(Quote)**, But here b is a mutable object,
# The added element is still the previous object, so the reference has not changed, but the value has changed.

Data structure, algorithm#

# coding: utf-8from collections import deque

classBNode:"""Binary Tree Node"""

 def __init__(self, value, left=None, right=None):
  self.value = value
  self.left = left
  self.right = right

def level_traverse(binary_tree):"""Hierarchical traversal binary tree"""
 stack =deque([binary_tree])while stack:
  top = stack.popleft()print(top.value)if top.left:
   stack.append(top.left)if top.right:
   stack.append(top.right)if __name__ =="__main__":
 b_tree =BNode(1,BNode(2,BNode(4,BNode(5,BNode(7)))),BNode(3,BNode(6, right=BNode(8))))level_traverse(b_tree)
# There are V-type and Y-type. If they cross, the last node must be the same, so the reverse traversal is performed directly from the last node.
# Reverse singly linked list
def reverse_single_link_lst(link_lst):if not link_lst:return link_lst
 pre = link_lst
 cur = link_lst.next
 pre.next = None
 while cur:
  tmp = cur.next
  cur.next = pre
  pre = cur
  cur = tmp
 return pre

# Find intersection
def point(node_a, node_b):if node_a is None or node_b is None:return None
 next_a, next_b = node_a, node_b
 while next_a or next_b:if next_a.val == next_b.val:if next_a.next and next_b.next and(next_a.next.val == next_b.next.val):
    next_a, next_b = next_a.next, next_b.next
    continuereturn next_a.val
  next_a, next_b = next_a.next, next_b.next
 return None

# Construct a singly linked list
classNode(object):
 def __init__(self, value, next=None):
  self.val = value
  self.next = next

a =ListNode(1,ListNode(2,ListNode(3,ListNode(4,ListNode(5)))))
b =ListNode(7,ListNode(9,ListNode(4,ListNode(5))))

ra =reverse_single_link_lst(a)
rb =reverse_single_link_lst(b)point(ra, rb)
# output:
# 4
# Quick sort, lz was more complicated to write at the time, but it was the most common way of writing (the tension caused a few small bugs), as follows
def quick_sort(lst, start, stop):if start < stop:
  i, j, x = start, stop, lst[start]while i < j:while(i < j)and(lst[j]> x):
    j -=1if(i < j):
    lst[i]= lst[j]
    i +=1while(i < j)and(lst[i]< x):
    i +=1if(i < j):
    lst[j]= lst[i]
    j -=1
  lst[i]= x
  quick_sort(lst, start, i-1)quick_sort(lst, i+1, stop)return lst

After that, the interviewer akun gave a very concise notation, three-way multiplexing, the address is in Gist

def qsort(alist):"""
 quick sort(easy way, but more memory)
 test: python -m doctest qsort.py
 >>> import math
 >>> import random
 >>> size =100>>> alist =[random.randint(0, size *10)for i inrange(size)]>>> qlist =qsort(alist)>>> alist.sort()>>> assert qlist == alist
    """

 iflen(alist)<=1:return alist

 key = alist[0]
 left_list, middle_list, right_list =[],[],[][{i < key: left_list, i == key: middle_list, i > key: right_list}[
  True
 ]. append(i)for i in alist]returnqsort(left_list)+ middle_list +qsort(right_list)

database#

The internet#

operating system#

Programming questions#

# You can specify the number of retries until the function returns the correct result.
@ retry(retries=3)
def func(*args,**kw):try:
  # some action
  return True
 except:return False

It can be written like this,

from functools import wraps

def retry(retries=3):
 def timesf(func):
  @ wraps(func)
  def wrap(*args,**kw):
   i =0
   status = True
   while status and i < times:
    status =func(*args,**kw)
    i +=1return status
  return wrap
 return timesf

Recommended Posts

Python knowledge points
Python3 supplementary knowledge points
Python advanced knowledge points
Python crawler basic knowledge points finishing
Summary of knowledge points about Python unpacking
Knowledge points of shell execution in python
Basic knowledge of Python (1)
Python basic knowledge question bank
Noteworthy update points in Python 3.9
​Full analysis of Python module knowledge
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Python string
Python descriptor
Python basics 2
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5
Summarize the knowledge points of Centos7 system reinforcement