Depth copy
= = Compares the contents of the data, if it is true that the content is the same, the reference is different
= After the operation is executed, the reference address of the front and back two elements is the same
Shallow copy copy () List
Returns a new list
Reference types only copy reference addresses
Deep copy
Full copy. Include reference types
from copy import deepcopylst2=deepcopy.(lst)
Random number
import random
Randint (A, B)
Returns an integer between [a, b], closed interval
Choice (seq)
Randomly pick an element from an element of a non-empty sequence, such as
random.choice(range(10))
, randomly pick an integer from 0 to 9.
random.choice([1,3,5,7])
Randrange ([Start,] stop [, step])
Gets a random number in the collection that increments by the specified cardinality from within the specified range, the cardinality
The default value is 1.
Front closed rear Open
Random.shuffle (list)->none
Scrambled list elements in place
Sample (population, K)
Randomly remove k different elements from a sample space or a population (sequence or collection type), returning
A new list
Meta-group
Ordered set of elements
Initialization
t=tuple()t=()t=(1,)
Tuple queries are similar to lists
Tuple elements cannot be modified
Attention:
For tuples that contain complex objects, their reference addresses to complex objects cannot be changed, but the data contents of complex objects can be changed
Namedtuple
from collections import namedtuplePoint=namedtuple(‘Point‘,[‘x‘, ‘y‘])#Point=namedtuple(‘Point‘,‘x y‘) 第二种写法p1=Point(11,12)print(p1.x,p1.y)
Part of the source code:
if isinstance(field_names, str): field_names = field_names.replace(‘,‘, ‘ ‘).split()field_names = list(map(str, field_names))typename = str(typename)
String Exercise Analysis: 1. Use the array to achieve the number of prime numbers within 100,000:
prime = [2]for i in range(3,100000,2): flag = False up = int(i**0.5)+1 for j in prime: if i % j == 0: flag = False break if j >= up: flag = True break if flag: prime.append(i)print(len(prime))
2. Yang Hui triangle: Method One (1) after iteration, add element 1:
n=6pre=[[1]]for i in range(n): line=pre[i] temp=[1] for j in range(i): temp.append(line[j]+line[j+1]) temp.append(1) pre.append(temp)print(pre)
Method One (2):
n = 6pre = [1]for i in range(n): temp = [1] for j in range(i): temp.append(pre[j] + pre[j + 1]) temp.append(1) print(pre) pre = temp
Method two list before or after complement element 0 iteration:
n=6pre=[1]for i in range(1,n): pre.append(0) newline=[] for j in range(i+1): var=pre[j-1]+pre[j] newline.append(var) print(newline) pre=newline
Method Three (1) Create a well-width, full 1 list in advance, replace the calculated result with the number of corresponding positions in the list, and replace the symmetric two elements with a symmetric loop:
n=6triangle=[]for i in range(n): temp=[1]*(i+1) for j in range(1,i//2+1): var=triangle[j-1]+triangle[j] temp[j]=var if i!=2*j: temp[-j-1]=var triangle=temp print(temp)
Method Three (2):
n = 6triangle = []for i in range(n): temp = [1] * (i + 1) triangle.append(temp) for j in range(1,i // 2 + 1): var = triangle[i - 1][j - 1] + triangle[i - 1][j] temp[j] = var if i != 2 * j: temp[- j - 1] = var print(temp) pre = temp
Method Four only opens up the maximum length of a list filled with 1, adding a temporary variable to store the replacement element, participate in the next calculation, take the symmetry, notice when changing the symmetric element, its index value to add an offset, the input when the list slices:
n = 6triangle = [1] * nfor i in range(n): temp = 1 for j in range(1,i // 2 + 1): var = temp + triangle[j] temp = triangle[j] triangle[j] = var if i != j * 2: triangle[- j - (n - i)] = var print(triangle[: i + 1])
Bubble Method Sort
Time complexity O (n**2)
Method One:
lst=[9,8,7,6,5,4,3,2,1]cnt=0cnt_swap=0for i in range(len(lst)-1): for j in range(len(lst)-i-1): cnt+=1 if lst[j]>lst[j+1]: lst[j],lst[j+1] = lst[j+1],lst[j] cnt_swap+=1print(lst,cnt,cnt_swap)
Method Two (Optimization): Optimization point: If after the end of an inner loop, there is no element exchange operation, the remaining elements are already in order, no need to go down
#lst = [9,8,7,6,5,4,3,2,1]#lst = [1,2,3,4,6,5,7,8,9]lst = [2,1,3,4,5,6,7,8,9]cnt = 0cnt_swap = 0for i in range(len(lst) - 1): flag = False for j in range(len(lst) - i - 1): cnt += 1 if lst[j] > lst[j + 1]: lst[j],lst[j + 1] = lst[j + 1],lst[j] flag = True cnt_swap += 1 if not flag: breakprint(lst,cnt,cnt_swap)
Python Second week study notes (1)