Python interview one hundred questions - comprehensive title (2)

table of Contents

  1. Shallow copy (copy) and a deep copy (DeepCopy)
  2. Write a generator, a two-dimensional list will be converted into a one-dimensional list
  3. Please write a recursive generator, arbitrary multidimensional lists into one-dimensional list
  4. How to get the current date and the date of each day of the year
  5. Communication between processes
  6. How to pass parameters to a thread function
  7. How to create and use global objects in a thread
  8. Do you know coroutine

0.1 shallow copy (copy) and a deep copy (DeepCopy)

Here Insert Picture Description

'''
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
Here Insert Picture Description

0.2 preparation of a generator, the list into a two-dimensional one-dimensional list

Here Insert Picture Description

'''
生成器: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
Here Insert Picture Description

0.3 Please write a recursive generator, arbitrary multidimensional lists into one-dimensional list

Here Insert Picture Description

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
Here Insert Picture Description

0.4 How to get the current date and the date of the first few days of the year, respectively,

Here Insert Picture Description

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
Here Insert Picture Description

0.5 Communication between processes

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
.join () wait, do not write .join () performs the following immediate

0.6 How to pass parameters to a thread function

Here Insert Picture Description
Here Insert Picture Description
to sum up
Here Insert Picture Description

0.7 How to create and use global objects in a thread

Here Insert Picture Description
The global variable only for the current thread can not be shared among multiple threads
Here Insert Picture Description
summary
Here Insert Picture Description

0.8 You understand it coroutine

Here Insert Picture Description
A topic
Here Insert Picture Description
title two
1.run
Here Insert Picture Description
2.create_task
Here Insert Picture Description
summary
Here Insert Picture Description

Published 21 original articles · won praise 5 · Views 1561

Guess you like

Origin blog.csdn.net/qq_36551226/article/details/104581048