测验

  1. 使用while循环实现for循环底层原理

# 提示:使用下述代码可以捕捉异常

try:
 # 逻辑代码
except StopIteration:
 break
 
# 结果为:
lis = [1, 2, 3]
lis_iter = lis.__iter__()
while True:
   try:
       print(lis_iter.__next__())
   except StopIteration:
       break
  1. 阅读需求,编写代码:

    1. 规定使用filter()内置函数和匿名函数

    2. names = ['Nick', 'Sean_sb', 'Tank_sb', 'Jason_sb']中留下以sb结尾的名字

names = ['Nick', 'Sean_sb', 'Tank_sb', 'Jason_sb']

# 结果为:['Sean_sb', 'Tank_sb', 'Jason_sb']
list(filter(lambda x: x.endswith('sb'), names))
  1. 阅读需求,编写代码:

    1. 自定义生成器

    2. 该生成器拥有range()函数拥有的功能

def self_range(start, end, step):
   while start < end:
       yield start
       start += step

# 结果为:

for i in self_range(1, 10, 2):
 print(i)  # 1,3,5,7,9
  1. 阅读需求,编写代码:

    1. 自定义3*4的numpy数组,

    2. 把第1行和第2列的元素置为0

import numpy as np

# 结果为:

'''
array([[0.       , 0.       , 0.       , 0.       ],
      [0.22721118, 0.       , 0.87410034, 0.85833497],
      [0.40389177, 0.       , 0.42199234, 0.87709706]])
'''
arr = np.random.rand(3, 4)
arr[0, :] = 0
arr[:, 1] = 0
  1. 阅读需求,编写代码:

    1. 对于字符串Life234234is beautiful234because234of persistence

    2. 请使用re模块 一行代码 还原这句话为Life is beautiful because of persistence

      import re
      
      s = 'Life234234is    beautiful234because234of    persistence'
      
      # 结果为:Life is beautiful because of persistence
      print(re.sub('d+|s+', ' ', s))
原文地址:https://www.cnblogs.com/whnbky/p/11405471.html