table of Contents
- Shallow copy (copy) and a deep copy (DeepCopy)
- Write a generator, a two-dimensional list will be converted into a one-dimensional list
- Please write a recursive generator, arbitrary multidimensional lists into one-dimensional list
- How to get the current date and the date of each day of the year
- Communication between processes
- How to pass parameters to a thread function
- How to create and use global objects in a thread
- Do you know coroutine
0.1 shallow copy (copy) and a deep copy (DeepCopy)
'''
copy:只复制深层对象的引用
deepcopy:复制深层对象的本身
'''
import copy
a = [1, 2, 3, 4, ['a', 'b']]
c = copy.copy(a) # 浅拷贝
d = copy.deepcopy(a) # 深拷贝
a.append(5) # 在顶层,无论浅还是深,顶层都完全复制
print(c) # [1, 2, 3, 4, ['a', 'b']]
print(d) # [1, 2, 3, 4, ['a', 'b']]
a[4][0] = 'x'
print(a) # [1, 2, 3, 4, ['x', 'b'], 5]
print(d) # [1, 2, 3, 4, ['a', 'b']]
to sum up
0.2 preparation of a generator, the list into a two-dimensional one-dimensional list
'''
生成器:yield
'''
def myGenerator():
numlist = [1, 2, 3, 4 , 5, 6, 7]
for num in numlist:
yield num
for num in myGenerator():
print(num, end=' ') # 1 2 3 4 5 6 7
[[1, 2, 3], [2, 3, 4]] ---> [1. 2. 3. 2. 3. 4]
def enumList(nestedList):
for subList in nestedList:
for element in subList:
yield element
nestedList = [[1, 2, 3], [2, 3, 4]]
for num in enumList(nestedList):
print(num, end=' ')
1 2 3 2 3 4
to sum up
0.3 Please write a recursive generator, arbitrary multidimensional lists into one-dimensional list
def enumList(nestedList):
try:
for subList in nestedList:
for element in enumList(subList):
yield element
except TypeError:
yield nestedList # 迭代单个值
nestedList = [4, [1, 2, 3], [2, 3, 4, [1, [2, 5], [2, 3], 5], 6]]
for num in enumList(nestedList):
print(num, end=' ')
4 1 2 3 2 3 4 1 2 5 2 3 5 6
to sum up
0.4 How to get the current date and the date of the first few days of the year, respectively,
import time
localtime = time.localtime(time.time())
print(localtime.tm_year) # 2020
print(localtime.tm_mon) # 2
print(localtime.tm_mday) # 29(这个月第29天)
print(localtime.tm_hour) # 21
print(localtime.tm_yday) # 60(今年第60天)
to sum up
0.5 Communication between processes
.join () wait, do not write .join () performs the following immediate
0.6 How to pass parameters to a thread function
to sum up
0.7 How to create and use global objects in a thread
The global variable only for the current thread can not be shared among multiple threads
summary
0.8 You understand it coroutine
A topic
title two
1.run
2.create_task
summary